Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Callback animation to begin only after ALL children have finished animating

Tags:

jquery

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 current code below, the #Message div starts fading in after the first child element and is actually placed after the last child. Once the last child fades out completely, the #Message div then "jumps" up into position. I want to avoid this "jump".

$('#DIV').children().fadeOut("slow", function() {
    $('#Message').fadeIn("slow");
});

How can I make certain the fadeIn() animation doesn't begin until fadeOut() is complete on ALL child elements of #DIV?

Edit: I should note that my #Message div is located inside of #DIV.

like image 974
jrrdnx Avatar asked Sep 16 '11 20:09

jrrdnx


1 Answers

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");
});
like image 155
rkw Avatar answered Nov 03 '22 06:11

rkw