This one stumped me for a bit, but I actually thought it would be something good to know in the future for myself, so I did some research for you. There were no clear answers online, some complicated solutions, but I wanted something fairly simple which I finally had to create myself. You can see the example of the
jQuery Delayed Consecutive Animations that I created working here:
examples/jquery-consecutive-animations/Each element will have its own jQuery animation and there will be a delay before the next one will start. I wrote the script in a way that it does not matter how many elements you have, it will go through all of them in order until it reaches the end of the animation where it calls the JavaScript clearInterval function to stop the loop. This is important otherwise it would continue the loop even after all of your animations have faded in. Visually you wouldn't notice a difference since executing the fadeIn function again wouldn't change anything since all of the elements have already faded in, however you would be wasting CPU resources in the browser at that point for your visitor. You want the loop to stop once they have all been iterated through, and that's the purpose for that. Just thought an explanation was necessary

The important part of the example I provided above is this:
$(document).ready(function() {
//Let's first hide all of the boxes
$('#container div').each( function() {
$(this).hide();
});
//Now lets select the first box
var box = $('#container div').first();
//Finally lets go through each box fading them in
var $id = setInterval(function() {
box.fadeIn(1000); //takes one second to fully fade in
box = box.next();
if(box.length == 0) {
clearInterval($id);
}
}, 500); //set delay between each animation to 0.5 seconds
});
- $(document).ready(function() {
- //Let's first hide all of the boxes
- $('#container div').each( function() {
- $(this).hide();
- });
-
- //Now lets select the first box
- var box = $('#container div').first();
-
- //Finally lets go through each box fading them in
- var $id = setInterval(function() {
- box.fadeIn(1000); //takes one second to fully fade in
- box = box.next();
- if(box.length == 0) {
- clearInterval($id);
- }
- }, 500); //set delay between each animation to 0.5 seconds
- });
I tested this in Firefox, Internet Explorer, and Chrome, and it seemed to work in each of the browsers. Originally I tried to do this using the callback function to do a recursive loop, however, JavaScript throws errors saying
too much recursion so that was out of the question.
By the way, if you want the animation to completely finish before the next one starts, you would need to set that 500 millisecond delay above to at least the animation's length which above is currently 1000 milliseconds. The way I did it above the next animation starts once the current animation is half way done.
Ozzu Hosting - Want your website on a fast server like Ozzu?