当动画时,只有当所有元素都完成后,回调如何触发? [英] When animating, how fire the callback only when all elements are done?

查看:88
本文介绍了当动画时,只有当所有元素都完成后,回调如何触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在jQuery中进行动画处理时,只有当所有元素都完成动画而不是每个元素都被触发时,才会触发回调的最佳做法是什么?

When animating in jQuery, what's best practice for firing a callback only when ALL elements are done animating and not for each element?

例如:

$('.someElements').fadeOut('fast', function() {
  // dont do this until ALL elements are done fading
}


推荐答案

要尝试的代码段:

var numberOfElements = $('.someElements').length;

$('.someElements').fadeOut(fast, function() {
  if( numberOfElements-- > 0 ) return;
  alert('call the Fireman!'); 
});

警报只是需要触发的结束回调的占位符。

The alert Is just a placeholder to the end-callback you need to fire.

EDIT(另一种方式):

EDIT (another way):

您还可以捕获所有元素,但不能捕获 > one。

You can also catch all the elements but not the last one.

$('.someElements').not(.someElements:last).fadeOut();

,然后添加一个带有回调的fadeOut到It

and then add a fadeOut with callback only to It

$('.someElements:last').fadeOut(fast, function (){ 
   // do something, it's the end of the world
};

这篇关于当动画时,只有当所有元素都完成后,回调如何触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆