How do I animate in jQuery without stacking callbacks?

后端 未结 7 1564
闹比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 05:56

    Use this:

    $('#div1, #div2, #div3').each(function(index){
        $(this).delay(1000 * index).hide(1000);
    });
    

    If you can give the

    s a class:

    $('.forHide').each(function(index, value){
        $(this).delay(1000 * index).hide(1000);
    });​
    
    • The first element fades out after 1000 * 0 = right away with animation of one second.
    • The second element fades out after 1000 * 1 = One second with animation of one second.
    • The third element fades out after 1000 * 2 = Two seconds with animation of one second.
    • ...
    • ...
    • The n element fades in after 1000 * n = n seconds with animation of one second.

    Live DEMO

提交回复
热议问题