I am animating html elements (cards) using jquery. I need to animate a dealing action ('one you for you, one for me, one you you, one for me...') so want the cards to fully animate (move position) before the next one starts. I am creating the cards in a $.each function and calling the animation from there (to avoid looping again unnecessarily). When the card is added to the page the animation starts but the loop then moves on to the next card without waiting for the animation to finish. This makes all the cards appear to animate at exactly the same time rather than sequentially. How do I make the loop wait for the animation callback? Could this be done with triggers? Could I manually queue the items?
A cut down version of my (erroneous) code:
var card = [
{ id:1, pos:{ x:100, y:100 } },
{ id:2, pos:{ x:150, y:105 } },
{ id:3, pos:{ x:200, y:110 } }
];
$.each(card, function() {
$('#card_' + this.id).animate({ top:this.pos.y, left:this.pos.x });
});
Demo @ http://jsbin.com/akori4