jQuery FadeOut many divs at once

Vinny picture Vinny · Apr 23, 2012 · Viewed 8.5k times · Source

I am trying to fade out multiple divs at once and fade in one div after that completes. Here's the code:

if($(this).attr("id")==="benefits-button"){

    $("#solar-about, #home-page, #process-page, #financing-page, #vendors-page, #consump-info-page, #smart-page, #wind-page, #about-page").fadeOut(750, function() {
         $("#benefits-page").fadeIn(750);
    });
    }

When there are multiple divs in the selector, the fadeOut and fadeIn happen at the same time.

Question: How do I get the fadeIn after the fadeOut?

Thank you

Answer

Okan Kocyigit picture Okan Kocyigit · Apr 23, 2012
$("#benefits-page").fadeIn(750);

is working immediately because it's starting to work when the first element (#solar-about in your example) fadeOut animation is completed.

If you want to wait until all animations are completed than you can use .promise(), like this:

$("#solar-about, #home-page, #process-page, #financing-page, #vendors-page, #consump-info-page, #smart-page, #wind-page, #about-page").fadeOut(750).promise().done(function() {
     $("#benefits-page").fadeIn(750);
});

DEMO