Simple Jquery Thumbnail Gallery

Posted on: June 21st, 2010 by James Vec

simple thumb gallery

The above image is what we will be creating. You can click on it to view a demo. It is a simple thumbnail gallery with a fade in fade out effect

If you want to skip the tutorial and just grab the source you can download it here.

All the photos were taken by Chloe Rice. Visit her here

All you need for this tutorial is 12 images, 6 thumbs and 6 full size. They can be any size you want. I used thumbs that were 80×80, and full size images that were 650×400. This is a very simple gallery and should not be used for large amounts of images as all images are loaded with the document.

As always we will need to link the Jquery files and the CSS file in the head.

<link rel="stylesheet" type="text/css" href="css/style.css" media="screen" />
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="js/simple_thumbs.js" type="text/javascript"></script>

After that we are ready to add the HTML to the body

<div id="wrapper">
  <div id="content">
  <div id="content_left">
  <div class="info_holder">
  <img src="img/title.jpg"  alt="simple gallery" class="title"/>
  <p class="by">Photos by: <span class="name">Chloe Rice</span></p>
  <p class="by">Visit her at <a href="http://ohchloe.com" class="h_link">Oh Chloe dot com</a>
  </p>
  </div>
<ul id="thumb_holder">
  <li><a href="javascript:void(0);"><img src="img/img1_thumb.jpg" alt="motherly" /> </a></li>
    <li><a href="javascript:void(0);"><img src="img/img2_thumb.jpg" alt="xo" /> </a></li>
    <li><a href="javascript:void(0);"><img src="img/img3_thumb.jpg" alt="keep your eyes open" /> </a></li>
    <li><a href="javascript:void(0);"><img src="img/img4_thumb.jpg" alt="bacon bits" /> </a></li>
    <li><a href="javascript:void(0);"><img src="img/img5_thumb.jpg" alt="nature sent packing" /> </a></li>
    <li><a href="javascript:void(0);"><img src="img/img6_thumb.jpg" alt="snow man" /> </a></li>
    </ul>
    </div>
    <div id="large_image_holder">
      <ul id="large_images">
        <li><img src="img/img1.jpg" alt="motherly" /></li>
        <li><img src="img/img2.jpg" alt="xo" /></li>
        <li><img src="img/img3.jpg" alt="keep your eyes open" /></li>
        <li><img src="img/img4.jpg" alt="bacon bits" /></li>
        <li><img src="img/img5.jpg" alt="nature sent packing" /></li>
        <li><img src="img/img6.jpg" alt="snow man" /></li>
      </ul>
    </div>

  </div>
</div>

The gallery markup is really just a couple of lists, one that contains the thumbnails and one that contains the full size images. For the plugin to work you must keep the two list ids the same. Other than that you can change whatever you want. #thumb_holder is the list that holds all the thumbnails and links. #large_images is the list that holds the corresponding full size images.

Next we will add the CSS to our CSS file. It is important to remember that the large images must be stacked for the effect to look good. So #large_image_holder must be relatively positioned so that we can position the main images absolutely within it. Everything else is really up to you. You can style it however you want.


html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {margin: 0;padding: 0;border: 0;outline: 0;font-size: 100%;vertical-align: baseline;background: transparent;}
body {line-height: 1;}
ol, ul {list-style: none;}
blockquote, q {quotes: none;}
blockquote:before, blockquote:after,
q:before, q:after {content: '';content: none;}

:focus {outline: 0;}

ins {text-decoration: none;}
del {text-decoration: line-through;}

table {border-collapse: collapse;border-spacing: 0;}

#wrapper{width:1000px; margin: 0 auto;}
#content{width:1000px; float:left; display:inline; margin:30px 0 0 0;}
#content_left{width:300px; float:left; display:inline;}

#large_image_holder{width:650px; float:left; display:inline; margin: 0 0 0 18px;}
#large_images{width:650px; float:left; display:inline; position:relative; z-index:10; padding:10px;  border:#464646 3px dashed; height:400px;}
#large_images li{position:absolute; z-index:11; left:10;}
#thumb_holder{width:300px;height:213px; float:left; display:inline; border:#464646 3px dashed;}
#thumb_holder li {width:80px; float:left; display:inline; margin:10px;}
.info_holder{width:300px;height:207px; float:left; display:inline;}

.title{float:left; display:inline; width:300px; }
.hide{display:none;}

.by{font-family:'Georgia',Times New Roman,Times,serif; text-transform:lowercase; color:#464646;font-style:italic; font-size:0.8em; line-height:1.1em; margin:0 0 0.5em 0;  }
.name{font-family:'Georgia',Times New Roman,Times,serif; text-transform:lowercase;color:#E62E25;font-style:italic;  }
.h_link{font-family:'Georgia',Times New Roman,Times,serif;color:#E62E25; font-style:italic;}

At this point if you linked my JS file at the top the gallery should already work. I will go over the Jquery block by block and explain what is going on. The effect is very simple to achieve and works on all browsers.

$(document).ready (function(){
	$("#large_images li").each(function(index, element){$(element).attr("class", 'hide');});
    $("#large_images li").each(function(index, element){$(element).attr("id", 'img'+index);});
    $("#thumb_holder li a").each(function(index, element){$(element).attr("rel", 'img'+index);});

The first lines of the script say that when the document is ready, we will add classes ids and rel attributes to certain elements on the page. The first loop loops through all the li objects in #large_images and adds the class hide. This way the large images are all hidden. The second loop adds unique ids to all the li objects. The last loop ads a rel attribute to each link which corresponds to the id of the images.

var mainImg ='img0';
	var current = 'img0';

Next we set some variables. We need to store the rel attribute in one so that we can pull the id of the image and we also need to know which image is the currently selected one so that we don’t apply the effect to it. You will see how these work in a moment.

$('#img0').css('display', 'inline');
	$('#img0').addClass('current');

These lines set the first image to be visible since we hid all of them in the loop. We want one image to be showing.

Below is the click function, where all the wizardry takes place

$('#thumb_holder li a').click (function(){
		mainImg = $(this).attr('rel');
		if(mainImg != current){
		$('.current').fadeOut('slow');
		$('#'+mainImg).fadeIn('slow', function(){
		$(this).addClass('current');
		current = mainImg;

		});
		}
	});
});

If you read through the script you can tell exactly what is going on. #thumb_holder li a, on click mainImg = $(this).attr(‘rel’). So it is grabbing the rel attribute from the a tag which is the same as the id for the image. So next we decide if we should use the effect. The if statement checks to make sure that mainImg is not equal to the current image, because if it is we don’t want anything to happen, because that image is already showing. If it isn’t then we fade out the current image and fade in the mainImg right on top of it. After all that we add the class current to the image that is selected, so we know which is the current image.

It is as simple as that, we now have a pretty neat little thumbnail gallery.

Grab the source you can download it here.

See the demo in action here

34 Responses

  1. You had some good ideas there. I made a search on the issue and noticed most peoples will agree with your blog.

    [Reply]

    James Vec Reply:


    July 2nd, 2010 at 3:20 pm

    Thanks Sarah I appreciate the feedback

    [Reply]

  2. Marcus

    Hey good post very useful stuff.

    [Reply]

  3. I must say I really like this gallery, it is so simple! Thanks for sharing your work on this, much appreciated.

    [Reply]

    James Vec Reply:


    October 22nd, 2010 at 11:36 am

    No problem man, I am glad that you enjoyed it

    [Reply]

  4. Rackham

    Thank you for sharing! One thing though, how would you add next and prev -buttons to the code as simply as possible? Might be a simple question, but I,m noob to JQuery. =)

    [Reply]

    James Vec Reply:


    October 22nd, 2010 at 5:07 pm

    You would have to add a function for that. You would just find the list item with the class of current, grab the next or previous list item, switch the image and then add the class of current to it.

    [Reply]

  5. Hi James,

    This gallery is wonderful, thanks for sharing! I’m building a photo retouching portfolio——any suggestions on how to implement a rollover, so that when the large image is moused over, it flips to a “before” image?

    Any ideas/links would be appreciated! Thanks a bunch!

    Katie

    [Reply]

    James Vec Reply:


    December 10th, 2010 at 10:56 am

    I think the easiest way to do this would be to set one of the images as a background image in the css and then you could fade out the image on top of it on roll over. Send me a link when your portfolio is done I would love to see what you did with it

    [Reply]

  6. Hey James, seriously nicely done! works perfect for a single gallery, but i wonder if somebody already managed to make it compatible with multiple instances of the gallery.
    Already trying changing id’s to classes, and replicating the JS code for different id’s.. but didn’t work out yet.

    Anyway i’ll keep trying.. ;)

    [Reply]

    James Vec Reply:


    February 3rd, 2011 at 3:08 pm

    This could easily be accomplished by using the jquery plugin format, that can be found
    here.
    I created this while learning Jquery so it is kind of primitive. If I was going to go back and rewrite it I am sure I would have done it much differently. Anyway, I am really glad you like it, and would love to see what you do with it, and sweet mustache sir

    [Reply]

  7. Erin

    Is there any chance you will update this tutorial with code that will allow multiple instances on a page? This gallery is perfect, and just what I was looking for. I’m not a programmer so my knowledge of javascript & jquery is pretty much cut & paste. What specific things would I need to change in the code and where, in order to make multiples of this gallery on one page?

    [Reply]

    jamesvec Reply:


    April 4th, 2011 at 2:30 pm

    I had been planning on converting it into a plugin but I have not started yet. This would allow multiple instances of the gallery on a single page.

    [Reply]

  8. Os

    Nice!!

    [Reply]

  9. Smashing work sir.

    Look forward to a plugin version sometime soon :)

    [Reply]

  10. suman

    hi there,

    really nice tutorials and exact what i am looking for. i am trying to implement this gallery in one of my page but i have different image size. is it possible to make this gallery whatever the image size the big image remains the in the middle of the container. any code or links will be highly appreciated and it will save load of headache for me.

    thanks for this tutorials

    suman

    [Reply]

  11. This is so simple and awesome! I am slowly starting to understand jQuery and this simple script + your notes are really helping!

    How would we add a class to the thumbnail that just got clicked? (So that it can get styled to show it’s the present one with CSS.)

    [Reply]

    Julie Blair Reply:


    September 17th, 2011 at 4:14 pm

    OMG I figured it out!

    [Reply]

    jamesvec Reply:


    September 17th, 2011 at 8:00 pm

    I am glad you figured it it out Julie!

    [Reply]

    Julie Blair Reply:


    September 17th, 2011 at 8:15 pm

    Just kidding — it added the class to ALL the thumbnails!

  12. To add a class tot he thumbnail that was clicked You would say
    $(‘#thumbholder li a’).click(function(){
    //to remove class from last clicked
    $(‘.name of class’).removeClass(‘name of class’);
    //to add class to current
    $(this).addClass(‘name of class’);
    })

    [Reply]

    Julie Blair Reply:


    September 17th, 2011 at 10:34 pm

    You sir, are a god among men!

    [Reply]

    jamesvec Reply:


    September 18th, 2011 at 12:57 pm

    glad I could help

    [Reply]

  13. Hi, awesome tool need to ask one thing i did use your script founding a problem when dynamic size of picture we are using its not coming in center its always showing in left any suggestion?

    thanks

    [Reply]

    JamesVec Reply:


    October 11th, 2011 at 12:20 pm

    I designed it with static images in mind. Maybe run it on window load instead of document ready?

    [Reply]

  14. Dudley

    Just wanted to say thanks for this great gallery.

    Thanks!

    [Reply]

  15. daniboombani

    I love this, it’s simple and looks great!

    Is it possible to have a small caption underneath each large image? So I can give each image a little description?

    [Reply]

  16. Carolina

    Hey I love your plugin is really simple to add but I’m trying to change it to show multiple galleries but it doesn’t work. Can you make an example for this? I will really appreciate it. =)

    [Reply]

    jamesvec Reply:


    November 30th, 2011 at 3:24 pm

    Hey carolina, when I wrote this I was still learning Jquery, and had not really used plugin syntax before. You would have to enclose this in a plugin template such as this one. http://css-tricks.com/snippets/jquery/jquery-plugin-template/ At some point I will go back and do this as many people have asked however I don’t have time at the moment to work on it :(

    [Reply]

  17. Sana

    Great tutorial, thanks!

    I’m a beginner and I would like to know, how can I add fading effects to the thumbnails? I would like to fade out all of the thumbnails and fade in the thumbnail, when I click on it and when it is active.
    Could you help me?

    Thanks

    [Reply]

Leave a Reply