2

我一直在尝试仅在元素的所有.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);
}
4

1 回答 1

7

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

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

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

$("#inner_work [class^='workBox_']").each(function() {
    // your existing animation code
}).promise().done(function() {
    ...
});
于 2013-01-02T17:01:13.830 回答