Seasonal Savings: 20% off selected memberships for a limited time. Give now

Learn it fast with expert-taught software and skills training at lynda.com. See what you can learn

By Ray Villalobos | Friday, February 10, 2012

Create a jQuery photo rotator in 15 lines of code

I love the jQuery tagline—write less, do more. It’s a perfect summary of what the language is about. jQuery is a JavaScript library that makes it easier to add interactivity on your projects. The nice thing about jQuery is that it takes care of dealing with browser inconsistencies and gives you great ways to access the DOM (Document Object Model). You can do amazing things in just a few lines of code.

The jQuery language is also easily extendable through the use of plugins. On this week’s episode of View Source, I will show you how to use jQuery Cycle, a jQuery plugin, to create an easy to code photo rotator for your site. The code is really easy to work with.

First, we’ll need some code with images. For example, create a list of images like this:

<div class="rotator"> <img src="photo1.jpg"> <img src="photo2.jpg"> <img src="photo3.jpg"> </div>

This is just a series of image tags, wrapped around a div with rotator designated as the class. Designating the class allows us to target our photos with the plugin.

You’ll need to call two libraries. The jQuery library as well as the jQuery Cycle plugin. You can do that with two lines of code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script src="http://cloud.github.com/downloads/malsup/cycle/jquery.cycle.all.latest.js"></script>

You should put this code either in the <head> section of your page or before your closing <body> tag. If you’re using WordPress, you can just add the second line into the header.php file of your template, because WordPress includes jQuery as a default.

Now for the jQuery part:

<script> $('.rotator').cycle({ fx: 'fade', timeout: 2000 }); </script>

In this part we use the <script> tag to start coding with JavaScript, then, we use jQuery to target our rotator class. With jQuery, you can target a class much in the same way you target a class with CSS, so we’re using a period in front of our class name to get to the rotator class. You then call on the cycle function, which takes just a few variables. In this case we’re calling on the type of effect we want and then setting how much time we prefer between fades.

Your finished product should look like this:

Car Photo 1Car Photo 2Car Photo 3

Here’s all of the code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script src="http://cloud.github.com/downloads/malsup/cycle/jquery.cycle.all.latest.js"></script> <script> $('.rotator').cycle({ fx: 'fade', timeout: 2000 }); </script> <div class="rotator"> <img src="http://i.imgur.com/fzC0Y.jpg" alt="Car Photo 1" /> <img src="http://i.imgur.com/L1BIC.jpg" alt="Car Photo 2" /> <img src="http://i.imgur.com/SsvZd.jpg" alt="Car Photo 3" /> </div> 

So there you have it, a photo rotator in less than 15 lines of code including the code for the images. Not bad for a few minutes of work.

If you want to find out how to add more options or how to work on more complicated layouts, check out this week’s free View Source tutorial, Creating a Photo Rotator with jQuery Cycle, in the Online Training Library®. If you are a lynda.com subscriber, also check out my second View Source tutorial, Using a CDN to speed up your sites, to find out how using CDNs (Content Delivery Networks) in your code can help make your pages faster.

Interested in more? • The full View Sourceseries • All developer courses on lynda.com • All web + interactive courses on lynda.com • All courses from Ray Villalobos on lynda.com

Suggested courses to watch next:• Create an Interactive Video Gallery with jQueryCreate an Online Photo Gallery with jQuery and DreamweaverCreate an Interactive Map with jQuery and DreamweaverjQuery Essential TrainingSet a Marquee to Autoplay with jQuery and Dreamweaver

Learn it from the experts.

With online video courses at lynda.com, you can reach your goals faster. Learn software, improve your skills, and get an inside look at how the professionals work.

See what you can learn


Share this article:

Get the latest news

  •   New course releases
  •   Pro tips and tricks
  •   News and updates
  
New releases submit clicked

You can change your email preferences at any time. We will never sell your email. More info

Featured articles

A lynda.com membership includes:

Unlimited access to thousands of courses in our library
Certificates of completion
New courses added every week (almost every day!)
Course history to track your progress
Downloadable practice files
Playlists and bookmarks to organize your learning
Become a member

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.