I'm trying to run each animation function one after the other instead of all at once.
This is what I've got so far:
$(document).ready(function(){
var bars = $('.bar');
bars.each(function(){
var widthpercent = $(this).attr("data-percent");
$(this).fadeIn();
$(this).animate({width:widthpercent},500);
});
});
I've tried using .delay()
and setTimeout()
in various combinations to no avail.
Could anyone point me in the right direction? Thank you!
It sounds to me like you're looking for
animate
'scomplete
function. You can write a recursive function to keep calling the function in thecomplete
function until all the items have been animated. To simplify: every time one element is animated, a callback is fired that animates the next element. That is the purpose of thecomplete
parameter, so I'm certain that is what you're looking for.Here's an example you can adapt to your specific needs.
Live demo here (click).
Further, this same logic can be applied to your
fadeIn
. Just wrap fadeIn's callback around that logic, like this:Live demo here (click).
And here's your code: live demo here (click).
This will animate after delaying
800 * i
milliseconds.See this JSFiddle example.
Try this:
Fiddle