Animate Panning Slideshow with jQuery

November 12th, 2010

If there’s one thing that never seems to go out of style, it’s a good jQuery slideshow.

Highly visual websites rely on the ability to showcase imagery automatically. Whether it be a professional photographer or zoo, slideshows pop up so frequently because they work well.

In today’s tutorial we’ll take the makings of a classic slideshow, but use a different kind of transition to animate between slides. It may not fit every project, but diversity is always welcome in the world of web design.

The Goal

Be the end of this tutorial, we’ll have a slideshow that transitions by changing the visible window. You will need to download two plugins for this tutorial: one for easing animations and one for timing slide changes. I’ve also packaged both in the tutorial source file below.

Panning Slideshow Result

Structuring the Slideshow

Step one is setting up the HTML structure for the slideshow.

As you may notice, we’re loading a few external JavaScript files into this page. After loading in the latest version of jQuery, we bring in the two plugins mentioned above. I’ve put both into a subfolder to keep things organized. The last JavaScript file loaded is “image-rotator.js”, and is where all of our custom jQuery code will go in a few steps.

Keeping with convention, we’ll contain the slideshow inside of an unordered list. This type of slideshow will require a helper “window” div in order to work correctly. Since we’re essentially looking at a specific corner of a larger image (simulated by the list) we will need to mask the areas outside of focus. This may sound a little complicated, but the image below should help you visualize:

Panning Slide Layout

Let’s get started by creating a new HTML file with the following code:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
  2.     "">  
  4. <html xmlns="" xml:lang="en" lang="en">  
  5. <head>  
  6.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>  
  8.     <title>Panning Slideshow | Build Internet Tutorial</title>  
  10.     <link rel="stylesheet" href="panning-slideshow.css"/>  
  12.     <script src=""></script>  
  13.     <script src="js/jquery.easing.1.3.js"></script>  
  14.     <script src="js/jquery.timer.js"></script>  
  15.     <script src="image-rotator.js"></script>  
  17. </head>  
  19. <body>  
  21.     <div id="window">  
  22.         <ul id="slideshow">  
  23.             <li class="box1"><img src="images/tiger.jpg" alt="Tiger"/></li>  
  24.             <li class="box2"><img src="images/macaw.jpg" alt="Macaw"/></li>  
  25.             <li class="box3"><img src="images/bald-eagle.jpg" alt="Bald Eagle"/></li>  
  26.             <li class="box4"><img src="images/panda.jpg" alt="Panda"/></li>  
  27.         </ul>  
  28.     </div>  
  30. </body>  
  31. </html>  

Style and Arrangement with CSS

The biggest change from typical slideshows is that the “#window” div is used to block out the full list images via overflow:hidden. Keep in mind that the grid of slideshow images is actually 1920px wide and 700px high. Since we’re only showing one slide at a time, the #window div cuts the visible area to 960px width and 350px height.

Copy the code below into your CSS file.

  1. *{margin:0; padding:0;}  
  3. body{background:#353637height:100%;}  
  5. #window{clear:bothwidth:960pxheight:350pxbackground:#131310overflow:hiddenposition:relativemargin:10px auto 10px auto;}  
  7. #slideshow{width:1920pxheight:700pxoverflow:hiddenposition:relative;}  
  8.     #slideshow li{width:960pxheight:350pxfloat:leftdisplay:inline;}  

As with any CSS file, there is always opportunity to condense and streamline the styles involved. If you end up making a more efficient version of this, please share it with the rest of us!

Animating with jQuery

The primary role of jQuery in this slideshow is to adjust the coordinates of the list item image being shown. Our roadmap is below:

Panning Motion Diagram

With this in mind, let’s turn these motions into jQuery animations. Copy the code below into your project’s JavaScript file, and then meet us below for the script explanation.

  1. $(document).ready(function(){  
  3.     //This keeps track of the slideshow's current location  
  4.     var current_panel = 1;  
  5.     //Controlling the duration of animation by variable will simplify changes  
  6.     var animation_duration = 2500;  
  8.     $.timer(6000, function (timer) {  
  9.         //Determine the current location, and transition to next panel  
  10.         switch(current_panel){  
  11.             case 1:  
  12.                 $("#slideshow").stop().animate({left: "-960px", top: "0px"}, {easing: 'easeOutBack', duration: animation_duration}); 
  13.                 current_panel = 2; 
  14.             break; 
  15.             case 2: 
  16.                 $("#slideshow").stop().animate({left: "0px", top: "-350px"}, {easing: 'easeOutBack', duration: animation_duration}); 
  17.                 current_panel = 3; 
  18.             break; 
  19.             case 3: 
  20.                 $("#slideshow").stop().animate({left: "-960px", top: "-350px"}, {easing: 'easeOutBack', duration: animation_duration}); 
  21.                 current_panel = 4; 
  22.             break; 
  23.             case 4: 
  24.                 $("#slideshow").stop().animate({left: "0px", top: "0px"}, {easing: 'easeOutBack', duration: animation_duration});  
  25.                 current_panel = 1;  
  26.             break;  
  27.             timer.reset(12000);  
  28.         }  
  29.     });  
  31. });  

This code starts out by declaring two variables. The first is current_panel, and will be used to keep track of the slideshow’s current location. The second is animation_duration and is used to affect all transitions in one place, rather than having to change times in four different locations. Where possible, it’s typically a good idea to condense attributes like this into variables for easy editing.

The timer function is based on our second plugin. Every 6000 milliseconds, the function encapsulated is fired. This function determines the current panel, and then animates the transition to the next sequential panel. Panel 4 automatically resets back to the start. Once the transition has been made, the current_panel variable is updated, and the timer is reset. Not bad!

What About Easing?

You may have noticed the easing attribute within the animate function. The easing plugin gives the transition a smooth elastic feel instead of a typical rigid motion. I’ve selected “easeOutBack” for the demo, but feel free to experiment with other easing options for your version.

Other Possibilities

Using this method, you could also setup one large slide instead of four individual ones. This would allow you to scan the contents of an oversized image in distinct zones, like a map.

Updating Delay

I started working on this slideshow before the delay function was added to the latest version of jQuery. While I haven’t explored it much yet, it seems like the timer plugin used here might be able to get replaced by it. If anyone chooses to explore this route, be sure to let us know what you come up with!

Have we missed anything/made any errors? Are there any steps that need further explanation? Let us know in the comments and we’ll try to sort it out!

The pictures of zoo animals used in this tutorial were all found via Flickr. Individual photo credits: Bald Eagle / Tiger / Panda / Macaw

Leave a Reply