【问题标题】:Trigger an event after all of the animations from current function completed在当前函数的所有动画完成后触发事件
【发布时间】:2012-12-17 01:15:06
【问题描述】:

我一直试图在元素的所有 .animate() 函数(包括延迟和缓动)完成后才触发函数。

我尝试了几种不同的方法,但都没有成功,有什么想法吗?

  $("#inner_work").on("mouseenter", ".activeBox", function(){
        var thisBox = $(this).attr('id');
        $("#inner_work [class^='workBox_']").each(function(){
            if($(this).attr('id') != thisBox){
                $(this).stop().delay(randomEasing(200,800)).animate({
                    opacity:0
                }, randomEasing(200,700));
            } else {
                $(this).stop().animate({
                    opacity:1
                }, 'fast');
            }
        }); 
  });

如何在所有动画完成后触发事件?

randomEasing就是这个功能让它随机交错

function randomEasing(from,to)
{
    return Math.floor(Math.random()*(to-from+1)+from);
}

【问题讨论】:

    标签: jquery animation random mouseenter easing


    【解决方案1】:

    您可以使用延迟对象并请求.promise() 来注册一个回调,该回调仅在集合中每个元素上的所有动画都完成时才被调用:

    $("#inner_work [class^='workBox_']").promise().done(function() { 
         ...
    });
    

    这可以与您现有的代码链接:

    $("#inner_work [class^='workBox_']").each(function() {
        // your existing animation code
    }).promise().done(function() {
        ...
    });
    

    【讨论】:

    • 附注使用this.id 而不是$(this).attr('id')
    • 我用过promise和done,如果我在一个jQuery集合中有10个元素,它需要10 *持续时间然后执行回调,还有其他方法吗?跨度>
    • @undefined 它不应该那样做。你有 jsfiddle 来证明这一点吗?
    • @Alnitak 谢谢!这太完美了!
    • 很遗憾,没有,实际上我有一个包含许多动画的项目,这并不总是发生。没关系,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    相关资源
    最近更新 更多