I thought it would be simple but I still can't get it to work. By clicking one button, I want several animations to happen - one after the other - but now all the animations are happening at once. Here's my code - can someone please tell me where I'm going wrong?:
$(".button").click(function(){
$("#header").animate({top: "-50"}, "slow")
$("#something").animate({height: "hide"}, "slow")
$("ul#menu").animate({top: "20", left: "0"}, "slow")
$(".trigger").animate({height: "show", top: "110", left: "0"}, "slow");
});
A slight improvement on @schmunk's answer is to use a plain object jQuery object's queue in order to avoid conflicting with other unrelated animations:
One thing to keep in mind is that, although I have never run into problems doing this, according to the docs using the queue methods on a plain object wrapper is not officially supported.
Animate Multiple Tags Sequentially
You can leverage jQuery's built-in animation queueing, if you just select a tag like body to do global queueing:
http://jsfiddle.net/b9chris/wjpL31o0/
So, here's why this works, and what it's doing:
The call to
$.globalQueue.queue()
is just queueing a call to your tag's animation, but it queues it on the body tag.When jQuery hits your tag animation in the body queue, your tag's animation starts, on the queue for your tag - but the way the jQuery animation framework works, any custom animation callback causes a tag's animation queue (the body's in this case) to halt, until the custom animation calls the passed-in
dequeue()
function. So, even though the queues for your animated tag and body are separate, the body tag's queue is now waiting for itsdequeue()
to be called. http://api.jquery.com/queue/#queue-queueName-callbackWe just make the last queued item on the tag's queue a call to continue the global queue by calling its
dequeue()
function - that's what ties the queues together.For convenience the
globalQueue.queue
method returns athis
reference for easy chaining.setInterval
For the sake of completeness, it's easy to land here just seeking an alternative to
setInterval
- that is you're not so much looking to make separate animations coordinate, as just fire them over time without the strange surge ahead in your animation caused by the way newer browsers will postpone animation queues and timers to save CPU.You can replace a call to
setInterval
like this:With this:
http://jsfiddle.net/b9chris/h156wgg6/
And avoid those awkward blasts of animation when a background tab has its animations re-enabled by the browser.
You can also put your effects into the same queue, i.e. the queue of the BODY element.
Make sure you call dequeue() within the last effect callback.
This has already been answered well (I think jammus's answer is the best) but I thought I'd provide another option based on how I do this on my website, using the
delay()
function...(replace 1000 with your desired animation speed. the idea is your delay function delays by that amount and accumulates the delay in each element's animation, so if your animations were each 500 miliseconds your delay values would be 500, 1000, 1500)
edit: FYI jquery's 'slow' speed is also 600miliseconds. so if you wanted to use 'slow' still in your animations just use these values in each subsequent call to the delay function - 600, 1200, 1800
I know this is an old question, but it should be updated with an answer for newer jQuery versions (1.5 and up):
Using the
$.when
function you can write this helper:Then you can call it like this:
You could do a bunch of callbacks.