【问题标题】:Callback after $.each with animations$.each 后的回调,带有动画
【发布时间】:2012-06-20 02:42:24
【问题描述】:

所以我正在运行一些动画来在我的第一个 jQuery 插件中引入一些 div:

$.fn.turnstile = function (options, callback) {

if (!this.length) {
    return this;
}
count = this.length;
var opts = $.extend(true, {}, $.fn.turnstile.defaults, options)
var delayIt = 100;
if (opts.direction == "in") {
    opts.deg = '0deg';
    opts.trans = '0,0';
    opts.opacity = 1;
} else if (opts.direction == "out") {
    opts.deg = '-90deg';
    opts.trans = "-100px, 200px";
    opts.opacity = 0;
} else if (opts.direction == "back") {
    opts.deg = '0deg';
    opts.trans = '-2000px,0px';
    opts.opacity = 0;
} else {
    opts.deg = direction;
}
this.each(function (index) {
    delayIt += opts.delayer;
    $(this).show().delay(delayIt).transition({
        perspective: '0px',
        rotateY: opts.deg,
        opacity: opts.opacity,
        translate: opts.trans
    }, 400, 'cubic-bezier(0.33,0.66,0.66,1)', function () {
        if (opts.direction == "back" || opts.direction == "out") {
            $(this).hide();
        }
        if (!--count && callback && typeof (callback) === "function") {
            if ($(":animated").length === 0) {
                callback.call(this);
            }
        }
    });
});
return this;
};

现在,我想在所有动画完成后调用我的回调,在数学上应该是 (delayIt+400)*count - 但我无法在所有动画完成后运行回调。正如您可能能够从其当前状态中看出的那样,我尝试检查:animated,使用!--count 条件,甚至尝试将超时设置为等于动画的持续时间,但似乎都是异步触发的.为这些设置动画并在完成后调用某些内容的最佳方式是什么?

Here's the info.transition() 插件上。

【问题讨论】:

    标签: javascript jquery css-transitions


    【解决方案1】:

    没有用你的代码测试过(我不熟悉.transition),但是试试这个:

        ...
        this.promise().done(function(){
            alert('all done');
        });
        return this;
    };
    

    【讨论】:

    • 我不知道你可以在每个 jQuery 对象上调用.promise :) 但是回调真的只有在每个动画完成后才执行,还是你必须以某种方式使用$.when
    • @FelixKling 在这种情况下我并不积极。我知道如果你使用$("ul > li").fadeIn(3000).promise().done(handler),当它们全部淡入时,处理程序会被调用一次。我不熟悉这种转换方法的工作原理。
    • 不管怎样,我都知道.promise(),但不确定它是否适合这个用例。谢谢!这个让人头疼:)
    • 谢谢。这就是我想知道的:)
    • 另外,添加了过渡插件信息,以防万一有其他方法可以做到这一点:)
    猜你喜欢
    • 2013-12-26
    • 2012-09-04
    • 1970-01-01
    • 2014-12-15
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多