2012-03-17 31 views
9

我一直在使用jQuery的stop(true, true)方法来清除正在运行的动画,以便下一个动画立即开始。我注意到第一个参数clearQueue清除了整个动画队列,但第二个参数jumpToEnd只跳转到当前正在运行的动画的结尾,而不是从队列中移除的动画。有没有办法让它清晰并跳到所有排队动画的末尾?jQuery停止(true,true)跳至队列中所有动画的结尾

我已经结束了链接几个stop(false, true)调用,但这只会处理3个排队的动画,例如。

$(this) 
    .stop(false, true) 
    .stop(false, true) 
    .stop(false, true) 
    .addClass('hover', 200); 

编辑:

我最终加入我自己的方法,stopAll,按照阿泰古拉尔的回答是:

$.fn.extend({ stopAll: function() { 
    while (this.queue().length > 0) 
     this.stop(false, true); 
    return this; 
    } }); 
$(this).stopAll().addClass('hover', 200); 

我希望别人认为这很有用。

回答

4

的jQuery 1.9引入了.finish()方法,它实现了这一点。

6

链接多个stop(false, true)是有道理的。而不是硬编码链式调用的一个固定数量,你可以检查队列长度:

while ($(this).queue().length) { 
    $(this).stop(false, true); 
} 

演示:http://jsfiddle.net/AB33X/

0

我也是从.finish()方法的jQuery中1.9的文档是

 
Animations may be stopped globally by setting the property $.fx.off 
to true. When this is done, all animation methods will immediately 
set elements to their final state when called, rather than displaying an effect. 

还有的.finish()文档页面上的各种方法,一个漂亮的演示通知。

0

在开始观看新动画之前,测试是否存在类(在悬停时设置并在mouseOut动画回调中移除)。当新的动画开始时,退出。

$("div").hover(function(){ 
    if (!$(this).hasClass('animated')) { 
     $(this).dequeue().stop().animate({ width: "200px" }); 
    } 
}, function() { 
    $(this).addClass('animated').animate({ width: "100px" }, "normal", "linear", function() { 
     $(this).removeClass('animated').dequeue(); 
    }); 
});