continuous movement animation with jquery

倖福魔咒の 提交于 2019-11-30 11:10:55

问题


continuous movement

I would like to recreate the truck moments in the above site , It is done in mootools. How would I code this, is there a jQuery plugin to do this?

So animate an object from beginning to end of screen and then it starts over again. How would I do this jQuery

Any help will e appreciated


回答1:


Here's a JSFiddle sample http://www.jsfiddle.net/XpAjZ/

More on jQuery animate: http://api.jquery.com/animate/

Demonstration shows 2 boxes animated across the screen at different speeds etc. (See fiddle)

Relevant jQuery Code:

var animateMe = function(targetElement, speed){

    $(targetElement).css({left:'-200px'});
    $(targetElement).animate(
        {
        'left': $(document).width() + 200
        },
        {
        duration: speed,
        complete: function(){
            animateMe(this, speed);
            }
        }
    );

};
animateMe($('#object1'), 5000);
animateMe($('#object2'), 3000);



回答2:


Here's an example of the following code.

This is relatively simple with jQuery using the position:absolute CSS property and the .animation()[DOCS] method callback. You will basically be animating the left CSS property over a period of time within a named function, then call that function in the animation callback when the animation is complete, like so:

var animateTruck = function () {

    $('#truck').css('left','-50px')
               .animate({'left':$(window).width()},
                        4000,
                        'linear',
                        function(){
                            animateTruck();
                        });
};

animateTruck();



回答3:


If you have a div with a picture inside, like this:

<div style="width: 1000px">
<img src="truck.png" id="truck" style="margin-left: -100px" />
</div>

Here is an jQuery example to slide the picture over the div/screen:

function moveTruck(){
    $('#truck').animate(
        {'margin-left': '1000px'}, 
        3000, // Animation-duration
        function(){
            $('#truck').css('margin-left','-100px');
            moveTruck();
        });
    }

moveTruck();

Hope that works out...




回答4:


How could you do this, yet have the content box slowly move back and forth across the width of the screen (not going through the screen and starting back at the other side)?



来源:https://stackoverflow.com/questions/4822524/continuous-movement-animation-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!