Submit your widget

jQuery Slideshow

Created 10 years ago   Views 22710   downloads 3710    Author devirtuoso
jQuery Slideshow
View DemoDownload 50.6 KB
Share |

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;


<div id="slideShow">
<div id="slideShowItems">
    <!-- Slide 1 -->
        <img alt="" src="image1.jpg" border="0" />
        <h2>Slide 1</h2>
        <p>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. </p>
    <!-- Slide 2 -->
        <img alt="" src="image2.jpg" border="0" />
        <h2>Slide 2</h2>
        <p>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.</p>
    <!-- Slide 3 -->
        <img alt="" src="image3.jpg" border="0" />
        <h2>Slide 3</h2>
        <p>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.</p>
    <!-- Add more tabs as you please.  jQuery will automatically add the tabs for you. -->

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 = '<ul id="slideShowCount">';  //Creates a ul list for tabs

//Create a tab for each slide.
for (var i = slides.length - 1;i >= 0 ; i--){
    html += '<li id="slide'+ i+'" class="slide"><span>'+(i+1)+'</span></li>' ;
html += '</ul>';

html =

<ul id="slideShowCount">
<li id="slide0" class="slide"><span>1</span></li>
<li id="slide1" class="slide"><span>2</span></li>



//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 =;
        //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);


I 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.