Create image gallery in 4 lines of jQuery

Few days ago friend asked me to build image gallery with thumbnails for her web page. Request was pretty simple, she wanted large image and few thumbnails bellow that. Also, large image should have bar with description. When user click on thumb large image and description should change.

gallery-4-lines

My idea was to use alt tag of thumb image for large image description. I already did that for jqFancyTransitions. Also, I was impatient to use jQuery’s delegate method in real life. So this is my image gallery with thumbs and description in 4 lines of jQuery.

First prepare images for gallery, large images and thumbs. We need some naming convention so let’s naming large images with suffix _large and their thumbs with suffix _thumbs. For every image we will have pair, image_xx_large.jpg and image_xx_thumb.jpg. In this example all large images will be 565×280px and thumbs will be 100×100px.

Next thing is to include jQuery in head of your document:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

After that we need layout to hold one large image and all thumbs. We need one panel with large image and description bar. Also, we need holder for thumbnails.

<div id="gallery">
	<div id="panel">
		<img id="largeImage" src="images/image_01_large.jpg" />
		<div id="description">First image description</div>
	</div>

	<div id="thumbs">
        <img src="images/image_01_thumb.jpg" alt="1st image description" />
        <img src="images/image_02_thumb.jpg" alt="2nd image description" />
        <img src="images/image_03_thumb.jpg" alt="3rd image description" />
        <img src="images/image_04_thumb.jpg" alt="4th image description" />
        <img src="images/image_05_thumb.jpg" alt="5th image description" />
	</div>
</div>

We should add some style to our gallery:

#thumbs { padding-top: 10px; overflow: hidden; }
#thumbs img, #largeImage {
 border: 1px solid gray;
 padding: 4px;
 background-color: white;
 cursor: pointer;
}
#thumbs img {
 float: left;
 margin-right: 6px;
}
#description {
 background: black;
 color: white;
 position: absolute;
 bottom: 0;
 padding: 10px 20px;
 width: 525px;
 margin: 5px;
}
#panel { position: relative; }

Now, when we have layout and CSS prepared we should add functionality to our image gallery. When user click on thumb, large image should be changed as well as description. We will replace ‘thumb’ to ‘large’ in src attribute of largeImage and use alt attribute of thumb for description. That can be achieved with following jQuery code:

$('#thumbs img').click(function(){
	$('#largeImage').attr('src',$(this).attr('src').replace('thumb','large'));
	$('#description').html($(this).attr('alt'));
});

And that’s it, you have fully functional image gallery for your site.

The better way for this is to use delegate method of jQuery. With delegate you’ll be able to dynamically load thumbs and use them without bind event listeners for new thumbs.

$('#thumbs').delegate('img','click', function(){
	$('#largeImage').attr('src',$(this).attr('src').replace('thumb','large'));
	$('#description').html($(this).attr('alt'));
});

Delegate method is added in jQuery 1.4.2. Check more about delegate().



Comments [ 81 Comments ]

  1. Joshica
    July 6, 2010

    This is cool. Thanks man

  2. Nick
    August 9, 2010

    THIS is the kind of stuff i like. Some image galleries out there that do almost the exact same thing are 10+ KB in size, WHEN THEY DON’T NEED TO BE. do more stuff like this, i love it.

  3. Robin Thrift
    August 13, 2010

    Agree, this a neat little chunk of code. I can definitely build up on that!

  4. Edward
    October 8, 2010

    Awesome, really clean!

  5. akil
    December 4, 2010

    Hi I am not sure how to use this plus but I have download the wp plugin but unsure how to implement this to show on up on the front page.

  6. jeip
    January 13, 2011

    Hi, I would like to open the thickbox by cklicking on the large image. how can I reach this?

  7. Big Steve
    February 20, 2011

    Very nice. I’m just learning jQuery and JS and this not only teaches me a lot, its just what I need for my first site.
    Thanks!!!

  8. Bjorn
    March 18, 2011

    Love it! Simply love it! Easy to implement and use! You are a Jquery god =)

  9. Sally
    March 20, 2011

    Hi, I have implemented this on my site, but when I click on the thumbnails they don’t change the large image. Am I missing something?

    I am using BigCommerce and wondering if I put the jquery in the wrong spot.

    Many thanks!

    -Sally

  10. Brian
    April 5, 2011

    Sally, I had a similar problem, I wasn’t escaping the dollar signs in the script section on my perl website. I recommend making an html page of this that works and then view the source on the non working one and the working html one and compare the two. You can see then where stuff is not being escaped properly(or other problems). Anyway thanks for this, looks and works great!

  11. StuartDesign
    April 11, 2011

    This is great! Nice work!

  12. Naveed
    May 6, 2011

    How to show a spinner/loading GIF image while the image loads? This gives a feedback to the users that something is happening.

  13. Lazarevic Ivan
    May 9, 2011

    You can try to add loading gif as background of #panel

  14. Alysa Lobalbo
    May 12, 2011

    Thanks for showing me, a “beginner”, there is much more in it! Keep making things easy and captivating and there will certainly be many many more people just like me knocking on this door.

  15. aditia
    May 23, 2011

    thanks man, that really easy to follow

  16. Mike
    June 3, 2011

    Hi,
    Great work. Is there a way to eliminate description/captions from appearing over the sliding images?
    Thanks.

  17. Catalin
    July 8, 2011

    Thank you! Very useful!

  18. Marge d'Wylde
    July 23, 2011

    Hi – Is there a way to put a link into the code so that when you click on each large image it can go to another page on the site?

    This will help me with a site that needs to be useful for dialup users.

    Warm regards,
    Marge d’Wylde

  19. Andrew B.
    August 22, 2011
    <a href="" rel="nofollow">
    
    </a>
    

    within the html place the image in the like above.

    var link = $(this).attr('data-id');
    $(".targetLink").attr('href',link);
    

    within the gallery jquery add these two lines under the forth line.It should look like this:

    $(document).ready(function(){
    $('#thumbs img').click(function(){
    $('#largeImage').attr('src',$(this).attr('src').replace('thumb','large'));
    $('#description').html($(this).attr('alt'));
    var link = $(this).attr('data-id');
    $(".targetLink").attr('href',link);
    });
    });
    

    the first 4 lines are the ones provided above and the last 2 are the ones that you add

  20. Adonice Web Design Group
    October 6, 2011

    it’s so nice ! thank you


Leave a Reply



Trackbacks and pingbacks
click to expand

  1. [...] bellow, large image and description should change when thumb is clicked. Same HTML and CSS code as here will be used. Dojo codesee how it worksMooTools codesee how it [...]

  2. [...] Canvas & jQuery( Demo | Download )Thumbnails Navigation Gallery with jQuery( Demo | Download )Create image gallery in 4 lines of jQuery( Demo | Download )WEB DEVELOPMENT( Demo | Download )jqGalViewII (Photo Gallery)( Demo | Download [...]

  3. [...] Create image gallery in 4 lines of jQuery (演示 | 下载) [...]

  4. [...] Create image gallery in 4 lines of jQuery (demo | download) [...]

  5. [...] Create image gallery in 4 lines of jQuery [...]

  6. [...] Image  Gallery in 4 lines of jQuery [...]

  7. [...] Create image gallery in 4 lines of jQuery (demo | download) [...]

  8. [...] Create image gallery in 4 lines of jQuery (演示 | 下载) [...]

  9. [...] Create image gallery in 4 lines of jQuery (演示 | 下载) [...]

  10. [...] Create image gallery in 4 lines of jQuery (演示 | 下载) [...]

  11. [...] Create image gallery in 4 lines of jQuery (演示 | 下载) [...]

  12. [...] Create image gallery in 4 lines of jQuery (演示 | 下载) [...]

  13. [...] Create image gallery in 4 lines of jQuery (演示 | 下载) [...]