15

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Subliminal Hash
  • 13,614
  • 20
  • 73
  • 104

8 Answers8

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

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andrew Bullock
  • 36,616
  • 34
  • 155
  • 231
5

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.

Tamas Czinege
  • 118,853
  • 40
  • 150
  • 176
  • 3
    Please, please, *please* just put `animationStep2`, instead of `function () { animationStep2(); }`. Why make a whole new function to just call another function? – Aistina Mar 28 '10 at 12:29
5

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

$("#elem").fadeIn(1000).delay(2000).fadeOut(1000);
Johannes Reiners
  • 570
  • 5
  • 11
1

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

Tader
  • 25,802
  • 5
  • 26
  • 27
0

Most jquery functions have a callback parameter, you can just pass a function into that.

Bryan A
  • 3,598
  • 1
  • 23
  • 29
0
$("#thisDiv").slideUp("slow", function() {
    // This function is called when the slideUp is done animating
    $(this).doNextThing();
});
Jiaaro
  • 74,485
  • 42
  • 169
  • 190
0

an example of this can be seen in JQuery for Beginners - Day 2

There are 15 days of these tutorials and they are a good resource. Enjoy!

Josh Mein
  • 28,107
  • 15
  • 76
  • 87
-1

Use the second parameter of the function: callback. For example,

$(this).slideDown( speed, function(){
    $(this).slideUp();
});
airportyh
  • 21,948
  • 13
  • 58
  • 72