Animate through a set of list items with jQuery

Animate through a set of list items with jQuery

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on January 27, 2012

Here’s an interesting code snippet I’ve found handy in past projects. Great for highlighting specific information within an application or web page. To start with lets have a brief explanation as to what we’re going to be doing. We have a set of items in a list, and we want to iterate though the list increasing and decreasing the font size. Here’s how it’s done.

The Code

Firstly we setup three variables that are used in the function, notice that we’re chaining the variables instead of declaring ‘var’ each time which is the optimal way of declaring multiple vars in jQuery. You can adjust the delay and animate values to suit your needs. We then loop through each list item using the eq(); method which allows us to select which list item to animate, you can see we’re passing the variable ‘i’ to this method which is increased towards the bottom of the script. Within the loop we finally call the function again to start the process again.


$(document).ready(function() {

         var i = 0,
         delay = 2000,
         animate = 400;
         function animateList(){
                 var imax = $("ul#list li").length -1;
                 $("ul#list li:eq(" + i + ")")
                         .animate({"fontSize" : "80px"}, animate)
                         .animate({"fontSize" : "80px"}, delay)
                         .animate({"fontSize" : "30px"}, animate, function(){

                                 (i == imax) ? i=0 : i++;
                                 animateList();
                         });

                 };

	   animateList();
});

The HTML


<ul id="list">
<li>Advertising</li>
<li>Art</li>
<li>Automotive</li>
<li>Business</li>
<li>Celebrity</li>
</ul> 

download


Recent shares

More tutorials from Papermashup
Comments
2 discussions around Animate through a set of list items with jQuery
  1. Mike Jones says:

    Why not just loop the ?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Looking for a registry cleaner to speed up your PC and show a full diagnostics?
Faster surfing with Dish Network High Speed Internet

Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in touch.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com you can find details here Or use the contact link below for further advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.