How do I animate in jQuery without stacking callbacks?

后端 未结 7 1576
闹比i
闹比i 2020-12-08 05:37

Let\'s say I have three divs, and I\'d like each to animate once the previous one is done. Currently, I write this:

$(\'div1\').fadeOut(\'slow\', function()          


        
7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-08 06:05

    If you're using a recent version of jQuery, use the animation promises:

    $('div1').fadeOut('slow').promise().pipe(function() {
        return $('div2').fadeOut('slow');
    }).pipe(function() {
        return $('div3').animate({ top: 500 }, 1000 );
    });
    

    You can make it generic:

    $.chain = function() {
        var promise = $.Deferred().resolve().promise();
        jQuery.each( arguments, function() {
            promise = promise.pipe( this );
        });
        return promise;
    };
    
    var animations = $.chain(function() {
        return $('div1').fadeOut('slow');
    }, function() {
        return $('div2').fadeOut('slow');
    }, function() {
        return $('div3').animate({ top: 500 }, 1000 );
    });
    
    $.when( animations ).done(function() {
        // ALL ANIMATIONS HAVE BEEN DONE IN SEQUENCE
    });
    

    Still a lot of function closures but that's the very nature of Javascript. However, it's much more natural and a lot more flexible using Deferreds/Promises since you avoid callbacks "inception".

提交回复
热议问题