Callback animation to begin only after ALL children have finished animating

后端 未结 2 1175
陌清茗
陌清茗 2021-01-13 04:08

I have a div wherein I would like to fade all of the child elements out at once, but fade in a new element but only after all children have completed fading out. Using my c

相关标签:
2条回答
  • 2021-01-13 04:30

    You'll want to use deferred objects specifically for scenarios like this. The easy part is that animations already create deferred objects by default: http://jsfiddle.net/rkw79/zTxrt/

    $.when(
        $('#DIV').children().each(function(i,o) {
            $(o).fadeOut((i+1)*1000);
        })
    )
    .done(function() {
        $('#Message').fadeIn("slow");
    });
    
    0 讨论(0)
  • 2021-01-13 04:34
    $('#DIV').children().fadeOut("slow", function() {
        if($('#DIV').children().filter(':animated').length == 1) $('#Message').fadeIn("slow");
    });
    

    Basically count how many are still animating, and when there is only one left, run the callback.

    This also makes your callback run just once, not once per element.

    0 讨论(0)
提交回复
热议问题