jQuery等待所有页面动画完成

Bri*_*ica 20 jquery animation promise

我知道如何等待动画完成

$('#element').animate(speed,function(){
//code here
});
Run Code Online (Sandbox Code Playgroud)

和多个元素

$('#element1, #element2').promise().done(function(){
//code here
});
Run Code Online (Sandbox Code Playgroud)

但我如何等待页面上的所有元素都完成动画?我宁愿不仅仅放入我在那里等待的每一个元素.

Jer*_*y T 46

要选择当前正在设置动画的所有内容,请执行$(":animated") http://api.jquery.com/animated-selector/

将它与你已经拥有的东西相结合,它就是

$(":animated").promise().done(function() {
    //code here
});
Run Code Online (Sandbox Code Playgroud)


Ton*_*one 10

Jeremy T给出的答案很好 - 尽管基于他链接的jquery网站上的评论(http://api.jquery.com/animated-selector/),为每个元素添加一个类是一个更快的解决方案在可能已设置动画的页面上,然后使用选择它们

    $('.animationclass').filter(':animated').promise().done(function() {
//Your function
});
Run Code Online (Sandbox Code Playgroud)