Quick Tip: Display Elements Sequentially with jQuery
Tutorial Details
- Topic: Sequential Animations
- Language: JavaScript
- Estimated Completion Time: 5 Minutes
- Post Graphic: Available at GraphicRiver
In this video quick tip, I’ll teach you how to add a bit of flair to your page, by displaying a set of elements sequentially. While there are numerous ways to accomplish this task, today, we’ll review one technique that uses recursive functions.
// Wrapping, self invoking function prevents globals
(function() {
// Hide the elements initially
var lis = $('li').hide();
// When some anchor tag is clicked. (Being super generic here)
$('a').click(function() {
var i = 0;
// FadeIn each list item over 200 ms, and,
// when finished, recursively call displayImages.
// When eq(i) refers to an element that does not exist,
// jQuery will return an empty object, and not continue
// to fadeIn.
(function displayImages() {
lis.eq(i++).fadeIn(200, displayImages);
})();
});
})();
Conclusion
What makes this usage effective is the fact that, when lis.eq(i) refers to an element that doesn’t exist in the wrapped set, an empty jQuery object will be returned. When this happens, the subsequent methods in the chain (fadeIn) will never be called.
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
- http://pippinspages.com Pippin
- http://matt-bridges.com/ Matt Bridges
- http://rommelcastro.me Rommel Castro A
- http://kodifica.com Massimo Lombardo
- http://kodifica.com Massimo Lombardo
- http://pixoliacreative.com/ Raymond Lopez
- anon
- SFdude
- http://www.decodize.com praveen vijayan
- http://posthope.com Paul
- Jhon
- http://www.thomas-online.no Thomas Bensmann
- http://websitecenter.ca/ Iouri Goussev
- http://websitecenter.ca/ Iouri Goussev
- http://www.umbraprojekt.pl/ mingos
- http://www.atomicrobotdesign.com Mike
- http://pixelcoder.co.uk Jeffrey
- http://shaneparkerphoto.com Shane Parker
- http://www.dazydude.net Rik de Vos
- http://www.daniel-petrie.com Daniel Petrie
- http://www.daniel-petrie.com Daniel Petrie
- chichibek
- Christian
- http://zecel.com Shishant Todi
- jeff
- Tobias Jurga
- http://nizzledev.com Ricardo Guillen
- http://alessio.no.de alessio
- http://www.chris-alix.com Pablinho
- http://www.daxhansen.com Dax
- http://www.matbaa-tr.com matbaa
- http://www.kase-tr.com kaşe
- regie
- http://javarevisited.blogspot.fr Java Geek
- http://www.facebook.com/mittul.chauhan Mittul Chauhan
- Gary
