jQuery多个animate()回调

Ale*_*lex 8 javascript jquery function callback jquery-animate

我正在尝试同时为一组元素制作动画(几乎每个动画之间都有一个小延迟):

$('.block').each(function(i){
   $(this).stop().delay(60 * i).animate({
     'opacity': 1
   }, {
      duration: 250,
      complete: mycallbackfunction // <- this fires the callback on each animation :(
   });
});
Run Code Online (Sandbox Code Playgroud)

所有动画完成后如何运行回调函数?

kcb*_*ner 5

在计数器变量周围使用闭包.

var $blocks = $('.block');
var count = $blocks.length;
$blocks.each(function(i){
   $(this).stop().delay(60 * i).animate({
     'opacity': 1
   }, {
      duration: 250,
      complete: function() {
        if (--count == 0) {
          // All animations are done here!
          mycallbackfunction();
        }
      }
   });
});
Run Code Online (Sandbox Code Playgroud)

请注意将项目列表保存到$ block变量中以保存查找.