jquery restart animation after it stops [duplicate

2019-09-08 05:58发布

问题:

Possible Duplicate:
Jquery Continuously Loop Animation

I want this animation to restart when the div timer reaches 940px. So far I have this:

$('.timer').animate({'width':940}, {queue:false, duration:5000, easing: 'linear'});
if ($('.timer').width() == 940){
    $('.timer').width() == 0;
    $('.timer').animate({'width':940}, {queue:false, duration:5000, easing: 'linear'});
}

回答1:

Restarting animated loop:

$(".timer").bind("animation.loop", function(){
    $(this).animate({width: 940}, 5000, function(){
        $(this).animate({width: 0}, 5000, function(){
            $(this).trigger("animation.loop");
        });
    });
}).trigger("animation.loop");

Do the animation, hard reset, animate again:

$(".timer").bind("animation.loop", function(){
    $(this).animate({width: 940, easing: 'linear'}, 5000, function(){
        $(this).css("width", 0).trigger("animation.loop");
    });
}).trigger("animation.loop");