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 :(
});
});
如何在所有动画完成后运行回调函数?
I'm trying to animate a set of elements simultaneously (almost, there's a small delay between each animation):
$('.block').each(function(i){
$(this).stop().delay(60 * i).animate({
'opacity': 1
}, {
duration: 250,
complete: mycallbackfunction // <- this fires the callback on each animation :(
});
});
How can I run a callback function after all animations have completed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在计数器变量周围使用闭包。
请注意将项目列表保存到 $block 变量中以保存查找。
Use a closure around a counter variable.
Note the saving of the list of items into the $block variable to save a lookup.
从 jQuery 1.5 开始,您可以使用
$.when
[ docs],这更容易编写和理解:演示
Since jQuery 1.5, you can use
$.when
[docs], which is a bit easier to write and understand:DEMO
Felix Kling 的回答将当没有动画可做时触发回调。如果动画通过
$el.stop()
停止,从而未完成到最后,这会使回调甚至触发。我找到了 Elf Sternberg 的方法< /a> 使用我在此 jsfiddle 中实现的延迟对象:
http://jsfiddle.net/8v6nZ/
Felix Kling's answer will fire the callback when there's no animation left to do. This makes the callback firing even if the animation is stopped via
$el.stop()
and thus not completed to the end.I found an method by Elf Sternberg using deferred objects which I implemented in this jsfiddle:
http://jsfiddle.net/8v6nZ/