在完成当前功能的所有动画后触发事件

use*_*442 2 random jquery animation mouseenter easing

我一直试图让一个函数只在所有元素的.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');
            }
        }); 
  });
Run Code Online (Sandbox Code Playgroud)

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

randomEasing 只是这个函数让它随机交错

function randomEasing(from,to)
{
    return Math.floor(Math.random()*(to-from+1)+from);
}
Run Code Online (Sandbox Code Playgroud)

Aln*_*tak 7

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

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

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

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