jQuery - Creating a Slideshow

7:57:00 AM |



To get things rolling we'll take a look at the html for the pretty example above. In the code below you will see a surrounding div (id slideshow-area) which holds our slideshow content scroll area and our next and previous buttons. Inside our scroll area we have a div to hold the content and finally the content itself. As far as html goes this is pretty simple stuff.
<div id="slideshow-area">
  <div id="slideshow-scroller">
    <div id="slideshow-holder">
      <div class="slideshow-content">
        <img src="eureka_small.jpg" />
      </div>
      <div class="slideshow-content">
        <img src="wallace_gromit_small.jpg" />
      </div>
      <div class="slideshow-content">
        <img src="dead_like_me_small.jpg" />
      </div>
    </div>
  </div>
  <div id="slideshow-previous"></div>
  <div id="slideshow-next"></div>
</div>
As I mentioned earlier you can replace the <img with any HTML content you want. That pretty much takes care of the html part.
CSS is up next, it is slightly more complicated than the html but nothing too crazy. The first piece to look at is the positioning and sizing of main slideshow area and scroller, which can have pretty much the same css. We do however add a border to our area to bring it out a little. The code below is fairly understandable, I make sure to set the position to relative so that I can easily position the next and previous buttons absolutely.
#slideshow-area, #slideshow-scroller {
  width: 500px;
  height: 500px;
  position: relative;
  overflow: hidden;
  margin: 0 auto;
}

#slideshow-area {
  border: 1px solid #000;
}
We are also going to set the height of the content holder to 500px, same as the area.
#slideshow-holder {
  height: 500px;
}
The next two items are the previous and next buttons. They take a little bit more work to make sure they are in the correct position. The buttons also have background images for pretty arrows (sorry IE6 users it may look bad since the arrows are png files). I also set the cursor to hand and pointer - for browser compatibility. And finally we also have classes to identify and float left each piece of content in the slideshow.
#slideshow-previous, #slideshow-next {
  width: 50px;
  height: 50px;
  position: absolute;
  background: transparent url("arrow_left.png") no-repeat 50% 50%;
  top: 225px;
  display: none;
  cursor: pointer;
  cursor: hand;
}

#slideshow-next {
  display: block;
  background: transparent url("arrow_right.png") no-repeat 50% 50%;
  top: 225px;
  right: 0;
}

.slideshow-content {
  float: left;
}
Well onto the real work, we now have to create the JavaScript to handle our functionality. jQuery makes this relatively simple though. First item is adding code to the document ready event.
var totalSlides = 0;
var currentSlide = 1;
var contentSlides = "";

$(document).ready(function(){
  $("#slideshow-previous").click(showPreviousSlide);
  $("#slideshow-next").click(showNextSlide);
 
  var totalWidth = 0;
  contentSlides = $(".slideshow-content");
  contentSlides.each(function(i){
    totalWidth += this.clientWidth;
    totalSlides++;
  });
  $("#slideshow-holder").width(totalWidth);
  $("#slideshow-scroller").attr({scrollLeft: 0});
  updateButtons();
});
The code starts out with a few variables that we will use. These hold the total number of slides, what slide we are currently on, and an array of content slides. Our document ready handler starts by adding click event handling to our previous and next buttons. We will define these functions shortly. Next, we figure out the total width of our content by using a simple selector on the slideshow-content class. We then run the results through jQuery's each function adding the widths as we loop through the items and also counting number of slides.
Next we need to create the two functions showPreviousSlide and showNextSlide. These two functions do mainly three things: change current slide number, update the buttons, and scroll the content. These functions along with support functions are below.
function showPreviousSlide()
{
  currentSlide--;
  updateContentHolder();
  updateButtons();
}

function showNextSlide()
{
  currentSlide++;
  updateContentHolder();
  updateButtons();
}

function updateContentHolder()
{
  var scrollAmount = 0;
  contentSlides.each(function(i){
    if(currentSlide - 1 > i) {
      scrollAmount += this.clientWidth;
    }
  });
  $("#slideshow-scroller").animate({scrollLeft: scrollAmount}, 1000);
}

function updateButtons()
{
  if(currentSlide < totalSlides) {
    $("#slideshow-next").show();
  } else {
    $("#slideshow-next").hide();
  }
  if(currentSlide > 1) {
    $("#slideshow-previous").show();
  } else {
    $("#slideshow-previous").hide();
  }
}
Starting with the last function, updateButtons, it handles showing and hiding the the appropriate buttons. It looks at what the current slide is and compares it to how many we have or if it is greater than one - pretty easy stuff. The next function is where all the work is done for scrolling our area. We first figure out where we need to scroll to. This is done by adding the width's of the previous slides together. Once the amount is calculated we just need to animate our scroller to the correct place using jQuery's animate function. We pass the attribute to change and how long it should take to do it. With these functions called by our previous and next button clicks we have our slideshow.
That wraps up this tutorial. I hope that at least one person finds something they need from this guy. As always if anyone has any questions feel free to drop us a comment or send us a question through our contact form. Until next time, keep killing time on the Internet.
Update 03/05/2009
I have updated the source code, there was a slight bug when refreshing the page or using the back buttons. This is fixed by resetting the scroll left on the scrolling div and calling an update on the buttons in our document ready function. The code above has been modified. Also here is the updated document ready function.
$(document).ready(function(){
  $("#slideshow-previous").click(showPreviousSlide);
  $("#slideshow-next").click(showNextSlide);
 
  var totalWidth = 0;
  contentSlides = $(".slideshow-content");
  contentSlides.each(function(i){
    totalWidth += this.clientWidth;
    totalSlides++;
  });
  $("#slideshow-holder").width(totalWidth);
  $("#slideshow-scroller").attr({scrollLeft: 0});
  updateButtons();
});