You can do this in a not-so-complicated way, like this:
$('#locations a').each(function(i) {
var left = $(this).css('left'),
top = $(this).css('top');
$(this).css({opacity: 0, left: 0, top: 0})
.delay(400 * i)
.animate({left: left, top: top, opacity: 1});
});
You can test it here. It's mostly simplification, the only important additions are .delay(400 * i)
and the i
to function(i) {
.
This just uses the i
provided as the first parameter to .each()
's callback and multiplies the .delay()
(delay before starting animation on this element) times that amount for each element. The 400
is for the default duration of .animate()
being 400ms. So the first element starts immediately, the next in 400ms, the next in 800ms, and so on...right when the animation prior to it should be finishing.
You can of course make a custom queue, etc...but this seems a bit simpler :)
Edit: since you're interested in building the queue, it'd look like this:
$('#locations a').each(function(i) {
var left = $(this).css('left'),
top = $(this).css('top'),
a = $(this).css({opacity: 0, left: 0, top: 0});
$(document).queue('myQueue', function(n) {
a.animate({left: left, top: top, opacity: 1}, n);
});
});
$(document).dequeue('myQueue');
You can test it here, we're using .queue()
to queue the functions in a custom queue up on document
then kicking it off with a .dequeue()
. The n
is the next function in the queue, we're calling that when the .animate()
finishes by providing it as the callback function to run.