本文介绍了jQuery的 - 推迟回调至多个动画完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一次后的多个元素完成动画的回调来执行。
我jQuery选择如下:

I need a callback to execute once after multiple elements have finished animating.My jquery selector is as follows:

$('.buttons').fadeIn('fast',function() {
   // my callback
});

这里的问题是,按钮类的一些元素,所有这些都需要被执行回调之前褪色中的匹配。目前的情况是,每个单独的元素完成动画后执行回调。这不是所希望的功能。我正在寻找一个优雅的解决方案,例如,经过所有匹配的元素完成动画我的回调只执行一次。这个问题在一些地方,包括SO弹起,但从来没有一个优雅的答案(甚至也没有为此事一个明确的答案 - 解决方案,为一个人工作,不为别人在所有的工作)。

The problem with this is that the buttons class matches a number of elements, all of which need to be faded in before the callback is executed. As it stands now, the callback is executed after each individual element has finished animating. This is NOT the desired function. I'm looking for an elegant solution such that my callback is only executed once after all the matched elements have finished animating. This question has popped up in a few places including SO, but there's never been an elegant answer (nor even a definitive answer for that matter - solutions that work for one person don't work at all for others).

推荐答案

要@罗斯的回答的一个替代方案,将始终触发的最后一个按钮回调淡入(这可能会或可能不会是最后一个按钮被告知动画)可以是:

An alternative to @Ross's answer that will always trigger the callback on the last button to fade in (which may or may not be the last button that was told to animate) could be:

var buttons = $(".buttons");
var numbuttons = buttons.length;
var i = 0;

buttons.fadeIn('fast', function() {
    i++;
    if(i == numbuttons) {
        //do your callback stuff
    }
});

这篇关于jQuery的 - 推迟回调至多个动画完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 21:51