$(document).ready(function() {
$("#div1").fadeIn("slow");
$("#div2").delay(500).fadeIn("slow");
$("#div3").delay(2000).fadeIn("slow");
$("#div4").delay(8000).fadeIn("slow");
});
This is my current setup but I feel like this isn't the best way to write this. I can't find any examples on how you would write this better for timing. Any help? I'd appreciate it.
I should also add that the timing of each element isn't consistent.
Not happy with the provided answers, here is what i used:
This way you don't have to declare the delays.
This can be done elegantly since 1.8:
http://jsfiddle.net/f3WzR/
fadeIn
takes a callback as its second parameter. That callback is executed as soon as the animation is complete. If you want the elements to fade in sequentially, you could chain the callbacks:This could be re-written using an array of selectors and a single method, if you felt like it:
See this last one in action at JSBin.
I'd do it in a loop, as long as you're talking about a consistent increment (and as long as they appear in the same order on the page).
Example: http://jsfiddle.net/km66t/
This uses the index passed by
.each()
to increment the delay.So you're effectively doing:
EDIT: To hopefully address the issue in the comment below, you could instead store an Array of the delays you want to use, and access the proper index of the Array using the index from
.each()
.