I want to use jQuery to fade in links inside a nav div on page load. Currently they're styled using an a and have individual id's.
#navtop a {etc.}
Will I have to change that into a class, add another one that hides the links and reapply the the class when the fadein starts or is there an easier method?
Also I'd like the links to fade in one after another.
HTML:
<div id="navtop">
<a id="link1" href="link.php"><img src="img/logotrans.gif" width="30" height="30" /></a>
<a id="link2" href="link.php">News</a>
<a id="link3" href="link.php">Blog</a>
<a id="link4" href="link.php">Tour</a>
<a id="link5" href="link.php">Photos</a>
<a id="link6" href="link.php">About</a>
<a id="link7" href="link.php">Contact</a>
</div>
This is as far as I've got with the JavaScript:
$(window).load(function() {
$('#link1').fadeIn(5000, function() {
// Animation complete
});
});
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.
nextQ_Item variable.fadeStagger delay time. We calculate the ending opacity by the ratio of the delay time to the overall time.dequeue() functionDoc.There is no need to change any of your markup. What you want can be achieved easily with the code below. Live example: http://jsfiddle.net/tw16/pJ5uC/
$(window).load(function () {
fadeA($('#navtop a:first-child'));
});
function fadeA(elem) {
elem.fadeIn(500, function () {
fadeA($(this).next());
});
}
I've used 500 rather than 5000 to speed up the viewing process.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With