Build Internet has a brand new theme, and that's only the beginning. Read the full story or hide this bar

Make a Fluid Thumbnail Bar with jQuery

Learn how to make a fluid thumbnail bar that pages through the images it contains.

Overview

The idea of a fluid thumbnail bar is simple: Create a list of thumbnails within a space where the overflow can be flipped through page by page. Speaking of pages, make sure we’re on the same one by checking out the demo page.

To be clear, this tutorial is intended to show you how to add this functionality to an existing list of images, in hopes you can use it to compliment whatever your current setup might be. This tutorial assumes you have some level of experience with jQuery.

Features

  • Cycle through thumbnails in different intervals depending on container width
  • Page arrows only displayed when needed (thumbnails overflow container)

Getting Started

First off, let’s make sure we have a base structure to work with, outlined below.

Scripts

jQuery (surprise!), Easing, and the separate file which will contain our script.

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.easing.min.js"></script>
<script type="text/javascript" src="js/fluid.thumbs.js"></script>

 

CSS

Just one stylesheet needed, nothing crazy. For the sake of brevity, I will let you review the stylesheet on your own, which is available in the download.

<link rel="stylesheet" href="css/fluid.thumbs.css" type="text/css" media="screen" />

HTML

Below is the structure of the list which will contain the thumbnails, wrapped in a container.

<div id="thumb-tray">
     <ul id="thumb-list">
          <li><img src="thumb-1.jpg"/></li>
          <li><img src="thumb-2.jpg"/></li>
          <li><img src="thumb-x.jpg"/></li>
     </ul>
     <div id="thumb-prev"></div>
     <div id="thumb-next"></div>
</div>

The jQuery

This is where the bulk of the functionality will be made possible, to get a good sense of what we’re aiming to do, here are the goals:

  • Automatically resize the width of thumbnail list to fit number of images it contains (when the window is loaded or resized).
  • Calculate how many thumbs are visible within the visible area.
  • Cycle through the pages of thumbs when the forward or back arrows are clicked.

The Variables

To make editing easier, we are going to plug the elements into variables. Additionally, we are defining the thumbInterval and thumbPage variables, which store the pixel width of each page and the pixel distance from zero, respectively.

var thumbTray = '#thumb-tray',
thumbList = '#thumb-list',
thumbNext = '#thumb-next',
thumbPrev = '#thumb-prev',
thumbInterval,
thumbPage = 0;

Setup

When the document is first ready, the width of the thumbnail list must be calculated, and then depending whether or not it exceeds to visible area, the thumb arrows are displayed and the pixel width of each thumb page is calculated.

// Adjust to true width of thumb markers
$(thumbList).width($('> li', thumbList).length * $('> li', thumbList).outerWidth(true));

The width is calculated by multiplying the width of one li by the total number of items within the list. The outerWidth function allows us to include padding and margins in our calculation.

// Hide thumb arrows if not needed
if ($(thumbList).width() <= $(thumbTray).width()) $(thumbPrev+","+thumbNext).fadeOut(0);

If the width of the thumbnail list is larger than the thumb tray, then the arrows are needed and faded in.

// Thumb Intervals
thumbInterval = Math.floor($(thumbTray).width() / $('> li', thumbList).outerWidth(true)) * $('> li', thumbList).outerWidth(true);

Similar to how the true width of the thumb list was calculated, we are now figuring out the width of each page of thumbnails, establishing how far to shift the list (left or right) when it is cycled through.

Thumb Navigation

Pages are navigated by adjusting the left property in a positive(backward) or negative(forward) direction. The thumbInterval variable stores what distance is considered a page, while the thumbPage variable stores the total distance from the left.

If the beginning or end of the list is reached, it will automatically spill over to the opposite end of the list. Each page is calculated based on how many complete list items can be visible in the current visible space. We make use of easing for these animations to add extra smooth transitions.

// Thumb Next Button
$(thumbNext).click(function(){
	if (thumbPage - thumbInterval <= -$(thumbList).width()){
		thumbPage = 0;
		$(thumbList).stop().animate({'left': thumbPage}, {duration:500, easing:'easeOutExpo'});
	}else{
		thumbPage = thumbPage - thumbInterval;
		$(thumbList).stop().animate({'left': thumbPage}, {duration:500, easing:'easeOutExpo'});
	}
});

// Thumb Previous Button
$(thumbPrev).click(function(){
	if (thumbPage + thumbInterval > 0){
		thumbPage = Math.floor($(thumbList).width() / thumbInterval) * -thumbInterval;
		if ($(thumbList).width() <= -thumbPage) thumbPage = thumbPage + thumbInterval;
		$(thumbList).stop().animate({'left': thumbPage}, {duration:500, easing:'easeOutExpo'});
	}else{
		thumbPage = thumbPage + thumbInterval;
		$(thumbList).stop().animate({'left': thumbPage}, {duration:500, easing:'easeOutExpo'});
	}
});

Handling Resizes

Since the thumbInterval and thumbPage are calculated when the page first loads, we need to adjust for the fact that when the browser window size changes, those numbers are no longer relevant. In order to keep this simple, when the window is resized, the variables are recalculated and the page is set back to zero.

$(window).resize(function(){

	// Update Thumb Interval & Page
	thumbPage = 0;	
	thumbInterval = Math.floor($(thumbTray).width() / $('> li', thumbList).outerWidth(true)) * $('> li', thumbList).outerWidth(true);
	
	// Adjust thumbnail markers
	if ($(thumbList).width() > $(thumbTray).width()){
		$(thumbPrev+","+thumbNext).fadeIn('fast');
		$(thumbList).stop().animate({'left':0}, 200);
	}else{
		$(thumbPrev+","+thumbNext).fadeOut('fast');
	}
	
});

Wrap-up & Expanded Script

I've taken the basics of this tutorial and expanded upon them in Thumb Reel, a free script I released that has added functionality such as clicking and keyboard navigation. You can also see this script used in the newest Supersized release, which is what it was originally made for, although it has since been modified.

Wordpress.com stats not installed! Posted Wednesday, June 22nd, 2011 / Back to Top

I this post. Tweet
SPONSOR

44 Comments 21 Mentions

  1. Alejandro Author Editor

    great fluids

    June 22, 2011 · Reply

  2. honlapkeszites Author Editor

    Nice effects! Thanks for sharing!

    June 23, 2011 · Reply

  3. Chris Author Editor

    awesome!!! thanks guys. also, could you add some more thumbnails to the demo? It doesn’t do anything by default on a wide monitor (working on a 27″ imac)

    June 23, 2011 · Reply

  4. joris de befashionlike Author Editor

    Thanks,

    We can add link to the thumbnails ?

    Best

    June 23, 2011 · Reply

    • Sam Dunn Author Editor

      Definitely, this is meant to be used to enhance whatever thumbnail list you may currently be using

      June 23, 2011 · Reply

  5. Origin Ideas Author Editor

    great tut, thx! but the demo lead to 404…

    June 23, 2011 · Reply

  6. Paweł P. Author Editor

    And why not act in HTML 5?
    Best regards!

    June 24, 2011 · Reply

  7. Siddhartha Sinha Author Editor

    Thanks for sharing this info and code

    June 24, 2011 · Reply

  8. Dave Author Editor

    Same as above…any chance that can be fixed?

    June 24, 2011 · Reply

    • Sam Dunn Author Editor

      Should be fixed now, thanks!

      June 24, 2011 · Reply

  9. AndyiBM Author Editor

    Same as Chris… my wide screen just shows a line of static thumbnails – thought it was broke. The link arrows only show up when I resize the screen smaller.

    June 25, 2011 · Reply

  10. Michael Author Editor

    Thank you very much this tutorial was very helpful and the thumbReel widget has some great additions. I have tried to incorporate this into a little Facebook app I am working on it seems to have trouble (does not receive any of the events and only loads a small portion of the images in the list). Can you think of any reason the thumbReel would have issues inside an IFrame?

    June 27, 2011 · Reply

  11. Giuseppe Author Editor

    Thanks for this fantastic tutorial

    June 30, 2011 · Reply

  12. affordable web designer Author Editor

    Nice effects! Thanks for sharing!

    July 6, 2011 · Reply

  13. Mick Author Editor

    Good question!

    July 22, 2011 · Reply

  14. Mick Author Editor

    I ment Michaels: Can you think of any reason the thumbReel would have issues inside an IFrame?

    July 22, 2011 · Reply

  15. Brian Wiltshire Author Editor

    Very informative, love this site !!

    August 26, 2011 · Reply

  16. shashank Author Editor

    Great site,ulti information and worth sharing Sam Dunn !!

    September 1, 2011 · Reply

  17. Dinesh Verma Author Editor

    How can i add this to my blogger blog?

    September 2, 2011 · Reply

  18. Rakesh Kumar Author Editor

    I always try to find out how people make this type of thumbnail. But this time i have a solution. Thanks

    September 9, 2011 · Reply

  19. Tiffany Author Editor

    Can we add captions to the pictures so that when the mouse hovers, you can see a title…? I tried to add it myself with jQuery Captify demo, but I’m ashamedly a novice and can’t seem to get it to work. Any help you could give me would be great.

    September 9, 2011 · Reply

  20. Shikeb Ali Author Editor

    Nice, it would be interesting to implement this on wordpress site. Scrolling post thumbnails.
    Thanks :)

    September 12, 2011 · Reply

  21. Namita Author Editor

    good share buddy .. :)

    September 17, 2011 · Reply

  22. Big Crunchy Author Editor

    You got here a nice collection of stuff!

    You should increase your number of thumbnails, because at 1920px wide doesn’t appear the arrows just because of 1/2 thumbnails missing.

    Good work!

    September 21, 2011 · Reply

  23. David Proctor Author Editor

    Is there a maximum number of thumbnails you can add? Obviously too man would create an issue with loading, but hypothetically, could you add several hundred without ill effect?

    September 25, 2011 · Reply

    • Sam Dunn Author Editor

      Yessir, the limit would be on the hardware side of things

      September 26, 2011 · Reply

  24. Gautch Author Editor

    Do you have a way to make this fluid, but maxed at 960px wide instead of full width?

    I cant seem to bet it to work right.

    October 10, 2011 · Reply

  25. Magnus Moholdt Author Editor

    Thank you for this! Great tutorial!

    February 15, 2012 · Reply

  26. Manish Author Editor

    Sir,
    I tried to include two bars o one page but only first one is working…2nd one is visible but its not working.. :(

    February 27, 2012 · Reply

  27. Used Forestry Trucks Author Editor

    nice effects and fluids. very thorough tutorial

    February 29, 2012 · Reply

  28. شات صوتي Author Editor

    You are great and thank

    March 6, 2012 · Reply

  29. Beats Headphones Author Editor

    Hi, Neat publish.There’s a issue with your web-site in net explorer, would verify this?? IE nevertheless would be the market leader and a large portion of people will miss your magnificent producing resulting from this predicament.

    March 28, 2012 · Reply

  30. Dhafian Author Editor

    This is cool man! Is this site using it for the thumbnail?

    June 15, 2012 · Reply

  31. Ali Author Editor

    Very nice example. Thanks for tutorial.

    October 22, 2012 · Reply

  32. ترموکوپل Author Editor

    Great site,ulti information and worth sharing Sam Dunn !!

    November 10, 2012 · Reply

  33. Bomb Shelter Construction Author Editor

    Hi, is there a way to make the thumbnails auto-scroll instead of having to click the thumb next arrow?

    thanks

    November 28, 2012 · Reply

  34. طراحی سایت Author Editor

    Good question!

    November 29, 2012 · Reply

  35. وی پی ان Author Editor

    good, i really thanks for this article

    February 2, 2013 · Reply

  36. سئو Author Editor

    Very Nice.

    سئو

    February 11, 2013 · Reply

  37. خرید شارژ Author Editor

    they are very helpful.nice effects and fluids.

    February 27, 2013 · Reply

  38. thelivenews Author Editor

    nice effects and fluids. very thorough tutorial

    March 10, 2013 · Reply

  39. طراحی سایت کرمانشاه Author Editor

    good! tnx

    March 12, 2013 · Reply

  40. ajcha Author Editor

    Is there a way to set the image size? I want to make the thumbnail larger than the current size on the demo..

    May 1, 2013 · Reply

  41. lesniks Author Editor

    Is there a way to set the image size? I want to make the thumbnail larger than the current size on the demo..

    May 3, 2013 · Reply

 

Join the Conversation

Back to Top / Comment RSS

2012 Build Internet. Created by One Mighty Roar. Icons by Komodo Media. Back to Top