开发者

jQuery - How to Prevent Animation Queue Buildup with Double Animations?

开发者 https://www.devze.com 2022-12-14 13:16 出处:网络
I was taking a look at this tutorial here to prevent animation buildups: http://www.learningjquery.com/2009/01/quick-tip-prevent-animation-queue-buildup

I was taking a look at this tutorial here to prevent animation buildups:

http://www.learningjquery.com/2009/01/quick-tip-prevent-animation-queue-buildup

My situation: I am doing a double animation like this below, and would like the prevent the animation build up. Since I have 2 animations going, it seems it ignores the .stop(). What can be done to prevent this? I have tried .stop() on both .animate and if I do that it stops animating...

$(document).ready(function() {
$('#element').hover(function() {
    $(this).stop()
        .animate(
            { left: 200 }, {
                duration: 'slow',
            })
        .animate(
            { top: 200 }, {
                duration: 'slow',
            });
} , function() {
    $(this).stop()
        .animate(
            { left: 0 }, {
                dur开发者_开发百科ation: 'slow',
            })
        .animate(
            { top: 0 }, {
                duration: 'slow',
            });
});

});

Any help will be greatly aprecaited!!


Use:

$(this).stop(true);

From stop():

Stops all the currently running animations on all the specified elements.

Note: "currently running" not queued, as you're experiencing. For that supply the parameter:

clearQueue (Optional) Boolean 

A Boolean (true/false) that when set to true clears the animation queue, effectively stopping all queued animations.

Also you can supply the second parameter that will shortcut to animation completion rather than cancelling it if that helps.


You should use .finish() which will not break the layout.

0

精彩评论

暂无评论...
验证码 换一张
取 消