Simple Image Slideshow with jQuery and jQuery Cycle Plugin

See The Demo

I recently completed a website design project for a local austin company that involved creating a simple image slideshow on the login page of their application. These types of slideshows have traditionally been done using an animation toolkit or framework such as Adobe’s Flash product. Starting around three years ago, I was introduced to script.aculo.us which provided the ability to use effects with DOM elements to provide cool animations.

Just within the last year, I’ve started working more with jQuery and have been introduced to the slew of extremely cool plugins.  The newest one I’ve come across is jQuery Cycle.  Using this plugin and 9 lines of javascript code, I was able to create an extremely simple but cool image slide-show for the website project for my client.

The first step in building the slideshow is to create the html element that will hold the images for your slideshow:

<div id=”slideshow”>
  <img src=”slides/slide-1.jpg” border=”0″/>
  <img src=”slides/slide-2.jpg” style=”display:none”/>
  <img src=”slides/slide-3.jpg” style=”display:none”/>
</div>

The above is a simple list of images within the div element that’s identified by the “slideshow” id.  This will enable jQuery to pick up the “slideshow” element and then iterate over the <img> elements within it when building the slideshow.

The second step is to write the actual javascript that will use the plugin to build and run the slideshow:

$(document).ready(function(){
  $(function(){
    $(“#slideshow”).cycle({
      fx: ‘fade’,
      speed:2500,
      timeout: 1000
    });
  });
});

A little explanation on what is happening here: The $(“#slideshow”) is grabbing the slideshow div element and feeding the images into jQuery Cycle Plugin’s initialization.  The plugin is applying the fade effect (see more information on the effects you can use here) at a speed of 2500 milliseconds and a timeout of 1000 milliseconds.  Speed controls how long the transition takes to complete and timeout controls the time between slide transitions.  You can find more information on the options for the plugin here.

All-in-all, this has turned out to be a ridiculously easy way to code up a simple slideshow.  You can enhance your slideshow in all sorts of ways such as loading your images through ajax after the page loads, randomizing ordering, and providing new transitions.  All without having to worry about motion tweens in Flash!

See the Demo

Tags: , , ,



Leave a Reply