Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finish one animation then start the other one

I have two divs and two separate links that triggers slideDown and slideUp for the divs.

When one of the divs are slided down and I click the other one, I hide the first div (slidingUp) and then open the other div (slidingDown) but, at the moment it's like while one div is sliding down, the other, also in the same time, is sliding up.

Is there a way that would tell jQuery to wait to finish sliding down of one div and only then start sliding up the other?

like image 580
Subliminal Hash Avatar asked Jan 20 '09 16:01

Subliminal Hash


4 Answers

$('#Div1').slideDown('fast', function(){
    $('#Div2').slideUp('fast');
});

Edit: Have you checked out the accordion plugin (if that's what you're trying to do)?

like image 62
Andrew Bullock Avatar answered Oct 24 '22 01:10

Andrew Bullock


You should chain it like this

function animationStep1()
{
   $('#yourDiv1').slideUp('normal', animationStep2);
}

function animationStep2()
{
   $('#yourDiv2').slideDown('normal', animationStep3);
}

// etc

Of course you can spice this up with recursive functions, arrays holding animation queues, etc., according to your needs.

like image 33
Tamas Czinege Avatar answered Oct 24 '22 03:10

Tamas Czinege


A bit cleaner example and with a delay between the animation:

$("#elem").fadeIn(1000).delay(2000).fadeOut(1000);
like image 30
player-one Avatar answered Oct 24 '22 02:10

player-one


You can use a callback to fire a next effect when the first is done:

$("#element1").slideUp(
    "slow", 
    function(){
        $("#element2").slideDown("slow");
    }
);

In this example the function defined as second argument to the slideUp method gets called after the slideUp animation has finished.

See the documentation: http://docs.jquery.com/Effects/slideUp

like image 26
Tader Avatar answered Oct 24 '22 01:10

Tader