You can queue up the fade-ins with a jQuery queue. Like so:
See it live at jsFiddle.
//--- Queue-up the fade-in's
var animationQ = $({});
$("#navtop a").each ( function () {
var jThis = $(this);
animationQ.queue ('FadeIns', function (nextQ_Item) {
jThis.fadeIn (5000, function() {
nextQ_Item ();
} );
} );
} );
//--- Start the fade-ins.
animationQ.dequeue ('FadeIns');
This approach has the advantage that you can queue up any collection of nodes, just by changing the selector. They do not have to be siblings.
Update:
To stagger the fade-in starts part-way in, use:
var fadeTime = 5000; //-- How long will an individual fade last? (mS)
var fadeStagger = 500; /*-- How far into i=one element's fade will
the next elements fade start? (mS)
*/
//--- Queue-up the fade-in's
var animationQ = $({});
$("#navtop a").each ( function (J) {
var jThis = $(this);
animationQ.queue ('FadeIns', function (nextQ_Item) {
jThis.fadeTo (fadeStagger, fadeStagger / fadeTime , function() {
nextQ_Item ();
jThis.fadeTo (fadeTime - fadeStagger, 1);
} );
} );
} );
//--- Start the fade-ins.
animationQ.dequeue ('FadeIns');
See it at jsFiddle.
Requested explanation:
We set up a generic queue container by jQuery-ing on an empty object ( $({})
). This seems cleaner than attaching the queue to some node.
We select our nodes with any valid jQuery selector, and then use the each()
functionDoc to loop through them.
For each node, we add an entry into our custom queue, using the queue()
functionDoc.
- We give the queue a unique name, "FadeIns," to avoid conflicts.
- Each queue entry will passed a pointer to the next queue entry, when it's called. We capture that with the
nextQ_Item
variable.
- The queue item, function has 2 jobs: (1) do the item fade, and (2) call the next queue entry when it is done.
- However, we have split the fadeDoc into 2 parts to accomplish the desired staggered-start.
- The first fade lasts only for the
fadeStagger
delay time. We calculate the ending opacity by the ratio of the delay time to the overall time.
- Now we trigger the next element.
- Next, we resume and complete the fade, remembering that it only has (total - stagger-delay) milliseconds left.
- Once our custom queue is built, we fire it off with the
dequeue()
functionDoc.