jQuery Slideshow

Homepages generally don’t have enough space for all the promotions clients want to put on there.  The homepage slideshow is a great space saving solution, while keeping your homepage from looking like a dollar store advert.  This post will walk you through the code of a jQuery slideshow.

The Slideshow

Before we start, it might be a good idea to show you the slideshow.  There is nothing too fancy, it has tabs, and it automatically rotates through all the slides.

The Code

Now for the meat and potatoes… or what I like to call it, the code.

The CSS:

/* background and font colors */
background-color: #111;
color: white;
font-family: Arial, "MS Trebuchet", sans-serif;


/* Define dimensions, and hide anything outside of the div wrapper.
This is so only the first news story displays as the other
stories are below the wrapper div if Javascript is shut off.*/

/*Specify a width for each news story */
div#slideShowItems div{

/* Make the news story image appear to the left*/
div#slideShowItems img {

/* Container for the tabs, the width is set so
when we float the tabs right they align with the
edge of the slideshow */

/* Float the tabs to the right, and give them a background
image that looks like a tab. */
ul#slideShowCount li.slide{
background: transparent url(tabs.jpg) no-repeat scroll left top;

/* Style the numbers inside of each tab */
ul#slideShowCount li.slide span{

/* Roll over / selected state for the tab */
/*Shifts background image up, there is a selected state
below the regular state in the image */
ul#slideShowCount li.slide:hover,
ul#slideShowCount li.slide.selectedTab{
background-position:left -18px;



Slide 1

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.


Slide 2

Laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.


Slide 3

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

The jQuery:

* When Everything is loaded */
$(document).ready(function() {

//Setup all the slides.
$('#slideShowItems div').hide().css({position:'absolute',width:'600px'});

var currentSlide = -1; //keeps track of current slide
var prevSlide = null; //keeps track of last selected slide.
var slides = $('#slideShowItems div'); // all the slides
var interval = null; //For setInerval
var FADE_SPEED = 500;  //How long it takes to transition.
var DELAY_SPEED = 15000; //How long each slide stays up.

var html = '

    ';  //Creates a ul list for tabs

    //Create a tab for each slide.
    for (var i = slides.length - 1;i >= 0 ; i--){
    html += '

  • '+(i+1)+'
  • ' ;
    html += '


html =

  • 1
  • 2


//Put tabs after slideshow warpper.

//Set the click event for each tab.
for (var i = slides.length - 1;i >= 0 ; i--){


//Sets the current slide to the one clicked.
currentSlide = event.data.index;

//Go to the slide.


//If there is 1 or less slides then hide the tabs.
if (slides.length <= 1){

//get things started.

//Goes to the next slide.
function nextSlide (){

//if the current slide is at the end, loop to the first slide.
if (currentSlide >= slides.length -1){
currentSlide = 0;

//Go to the slide.


//Go to the slide specified in the argument.
function gotoSlide(slideNum){

//If the slide they're trying to access isn't
//the currently selected slide...
if (slideNum != prevSlide){

//The very first slide the prevSlide will be null.
//No point in trying to hide the slide when it doesn't
//exist yet.
if (prevSlide != null){

//Hide previoius slide and deselect old tab.

//Select new tab.

//Display new slide.

//Make the currentSlide the old slide for next transition.
prevSlide = currentSlide;

//if the auto slide advance is set, stop it, then start again.
if (interval != null){

//Goes to next slide every couple of seconds.
interval = setInterval(nextSlide, DELAY_SPEED);


tried to comment the code as best as I could. I’ll just give you a quick rundown of the code. The CSS basically sets a width and height for the slideshow, so when it is transitioning between slides it doesn’t jump up and down. We also shove the tabs to the bottom right of the slideshow.

The HTML isn’t too difficult. Each news story is wrapped in a div. If you want to add more stories just create another div and the jQuery wil automatically add the tabs.

The jQuery basically figures out how many divs are within the wrapper, then creates an unordered list of the required tabs. It then creates a click event for each tab. This event just calls a function that switches out the tabs and slides. We also have a function that is called at a set interval if a tab isn’t clicked. It basically just loops through all the slides.

That’s pretty well all there is too it.

Download: http://www.htmldrive.net/items/download/415

Be Sociable, Share!

Leave a comment