Jquery 在当前函数中的所有动画完成后触发事件

Jquery 在当前函数中的所有动画完成后触发事件,jquery,animation,random,mouseenter,easing,Jquery,Animation,Random,Mouseenter,Easing,我一直试图在元素的.animate()函数(包括延迟和放松)全部完成后,才触发一个函数 我尝试过几种不同的方法,但运气不佳,有什么想法吗 $("#inner_work").on("mouseenter", ".activeBox", function(){ var thisBox = $(this).attr('id'); $("#inner_work [class^='workBox_']").each(function(){ if(

我一直试图在元素的
.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');
            }
        }); 
  });
如何在所有动画完成后触发事件

randomousing
就是这个函数,使它随机交错

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

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

$("#inner_work [class^='workBox_']").promise().done(function() { 
     ...
});
这可以与您现有的代码链接:

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

p、 使用
this.id
而不是
$(this.attr('id')
我使用了
promise
done
,如果我在一个jQuery集合中有10个元素,它需要10*的持续时间,然后执行回调,还有其他方法吗?@undefined它不应该这样做。你有JSFIDLE来证明这一点吗?@Alnitak谢谢!这太完美了!不幸的是,没有,实际上我有一个有很多动画的项目,这并不总是发生。没关系,谢谢。