Search code examples
javascriptjqueryhtmlcross-fade

Crossfade Two divs Using jQuery


I have a multi-step webform on a website. When a person clicks next the div one fades out as div two fades in. The problem I am seeing is while div one fades out div two fades in below div one and then jumps once div one fades out.

How do I prevent this and have them actually crossfade?

HTML

<div class="step-1"> 
  <!-- CODE -->
</div >

<div class="step-2"> 
  <!-- THIS DIV IS HIDDEN TO BEGIN WITH -->
</div >

JS

$( ".step-1-next" ).on( "click", function() {
    $( ".step-1" ).fadeOut( "slow" );
    $( ".step-2" ).fadeIn( "slow" );
    return false;
});

Solution

  • Try this:

    $( ".step-1-next" ).on( "click", function() {
        $( ".step-1" ).fadeOut( "slow", function(){
           $( ".step-2" ).fadeIn( "slow" );
          });
        });
    

    This will cause the fade in to wait to be called until the fade out is completed.