Simple jQuery Gallery

Simple jQuery Gallery

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on July 16, 2009

Recently I was asked to code a really simple photo gallery with jQuery, so I thought I’d post it for all to use. Its really simple and is the first time I’ve built a gallery with jQuery. It works by taking the link to the large image from the rel tag that we set in our html code. we then replace the contents of the div with the id of #image. The hide() and fadeIn() functions are optional. Check out the demo below and as usual you can download the complete code!

gallery

 $(function() {
$(".image").click(function() {
var image = $(this).attr("rel");
$('#image').hide();
$('#image').fadeIn('slow');
$('#image').html('<img src="' + image + '"/>');
return false;
	});
});

Below is the HTML. note that we use the rel attribute which holds the link to the full sized image. We take that attribute and get the value using JavaScript.

<div id="image"><img src="images/1.png" border="0"/></div>
<a href="#" rel="images/1.png" class="image"><img src="images/t1.png" class="thumb" border="0"/></a>
<a href="#" rel="images/2.png" class="image"><img src="images/t2.png" class="thumb" border="0"/></a>
<a href="#" rel="images/3.png" class="image"><img src="images/t3.png" class="thumb" border="0"/></a>

demodownload

More tutorials from Papermashup
Comments
41 discussions around Simple jQuery Gallery
  1. Ivan says:

    Awesome tutorial!

    I was wondering about implementing lightbox with this tutorial, i tried it with Colorbox http://colorpowered.com/colorbox/ , the result is not working because the lightbox doesn’t appear while the tag is correct.

    Div of gallery
    —————————————————————————————–

    —————————————————————————————–
    ————-Script———————-
    $(function() {
    $(“.imagechanger”).click(function() {
    var image = $(this).attr(“rel”);
    $(‘#imageset’).hide();
    $(‘#imageset’).fadeIn(‘slow’);
    $(‘#imageset’).html(‘‘);
    return false;
    });
    });
    ——————————————–

    Any help would be much appreciated!

  2. Beautiful an d simple Gallery! Thanks..

  3. Mark says:

    I really liked thats this is a basic thumbnail gallery, the only fallback is it doesnt work when javascript is disabled.

  4. Scott says:

    tried @pablo’s code to stop it jumping back to the top of the page and couldn’t quite get it to work so after a few tweaks i managed to get the following code to work :]

    $(function() {
    $(“.image”).click(function() {
    var image = $(this).attr(“rel”);
    $(‘#image .current img’)
    $(‘#image’).fadeIn(‘slow’);
    $(‘#image’).html(”);
    return false;
    });
    });

    thought i better share as a few people have been asking!

  5. Roger Walters says:

    Thank you so much for this code, so simple and does exactly what it says!

    However, if anyone knows how to stop it from jumping to the top of its parent div, it would be greatly appreciated :-)

  6. Pablo says:

    It is a very good work, Thank you very much,
    But if the gallery is not at the top of the page, if you click on an image the browser scroll moves up.
    I have solved the problem with this code:

    $(function() {
    $(“#gallery .image”).click(function() {
    var image = $(this).attr(“rel”);
    $(‘#gallery .current img’).attr(‘src’,image).hide().fadeIn(‘slow’);
    return false;
    });
    });

  7. Jesse says:

    RE: Hey Ashley,
    This gallery is simply genius. I have one quick question about it, though. Since you’re using to anchor the thumbnail links, I notice that it jumps back to the top of the page every time I click on one of the thumbnails, is there anyway around this? Thanks!
    -Ryan

    I am having the same issue as ryan, any way to fix the jumping anchorlinks???

  8. gtagamer says:

    Can you make a translation on this? :)

  9. Martin says:

    Hi… Love this, it’s grand. I’d echo the question mentioned above… how difficult would it be to have multiple galleries on one page (I’m doing a small ecommerce site where this’d be very useful).

  10. Greg says:

    Great job!! Amazing, very simple and easy to install.

    Only have just question:
    How can i make work transition effect between big images?

    thank you again for this perfect job

  11. RobbyDesigns says:

    Thank you very much for this, a fantastic and simple gallery which I installed in seconds. So many posers online trying to over complicate things and yet not bother to explain it properly, nice to see a true pro at work .

  12. selimoff says:

    Great Gallery, short code, and easy to inplement!
    2 things:
    1) //

    2) How can I add a navigation to the gallery?

    thanks Ashley

  13. Muhon says:

    @ Matt, Could you please provide the html with caption? I would be very glad.

  14. Pingback: Portfolio Simple Image Gallery

  15. Matt says:

    Great Gallery,

    Ive been after something simple and easy to implement! Great Work.

    Note: I also wanted to add in a caption to display under the image so if you wanted to do this just change your script like shown (Ive commented the changes)

    $(function() {
    $(“.image”).click(function() {
    var image = $(this).attr(“rel”);
    var title = $(this).attr(“title”); // Extra line to create the varible for the title
    $(‘#image-gallery’).hide();
    $(‘#image-gallery’).fadeIn(‘slow’);
    $(‘#image-gallery’).html(” + title + ”); // Edited the line so that the p tags with the class gal-title
    return false;
    });
    });

    Then just add the title attribute to your href tags in your html.

    Easy.

  16. Alaa Badran says:

    Awesome gallery, 5 star for the author.

    I also would like to share:
    Easy Gallery
    http://www.freelancer-id.com/easy-gallery/

    Thanks :)

  17. Andrew says:

    Thank you for making a quick and easy web gallery! After trying some others this did exactly what I wanted it to do.

  18. RW says:

    This is great! Just one question: I have a navigation bar on my page that stops working (the links aren’t clickable and none of the hover formatting etc appears). Any way around this?

    –RW

  19. I’ve been looking for something nice and simple like this. Thanks for sharing

  20. Jenn says:

    Love it!

    If I want to have each large image link to a unique website. Can this be done with this code?

    Thanks for sharing. :)

  21. Gary says:

    Gracias, me fue de mucha utilidad!

  22. Iarrieta says:

    Forget it, was a simple distraction with .JPG and .jpg

    Sorry

  23. Iarrieta says:

    Hi

    Very nice your script but I have a little problem I don’t understand. I use your code and in local server works but in online server don’t

    It’s very rare. What may be wrong?

  24. Steve says:

    Great gallery, it was easy to use and looks very slick. I was curious how to alter the code in order to have more than one gallery on the page? I’m assuming it’s a matter of editing the short script and changing the id’s for the second gallery to something like “image2″, but my attempts have been unsuccessful. Thanks

  25. Alfred says:

    Ashley… I put a flash video inside … and it works fine… now is a -basic- multimedia gallery too =D

    = ] Thanks again!

  26. Alfred says:

    Perhaps a preload images will be necessary?… Because until you’ve picked a thumb, the transition effect between big images don’t show… The first time is “estatic”

    Simple, efective, easy and great!… Thanks!!… Just that i need!!

  27. Jose luis Rivero says:

    Very nice website

  28. Nathaniel says:

    What happened to the description? I saw it appear in the demo. But all of a sudden … no description. What happened? Can you bring it back or send me the files?

    I love the simplicity of this gallery. Thanks for you help.

  29. Jake says:

    @ashley. Thanks, its ok for now. But how about if I want to include a description or even a title to the photos?

  30. Pingback: Simple jQuery Gallery – Un sencilla galería de imágenes - CSS | pixelco.us blog

  31. Pingback: jQuery gallery update with title and description | Papermashup.com

  32. Evulgo Media says:

    WAW!
    Just as i needed.
    Thank a lot!

  33. Yoosuf says:

    Cool, if you guys wanted to know step by step try out this

    http://eyoosuf.blogspot.com/2009/02/simple-gallery-with-jquery.html

  34. Jake says:

    do you have a working demo? its only a screenshot that I can see when I click your DEMO button.

  35. Pingback: CSS Brigit | Create a simple gallery with jQuery

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Looking for a registry cleaner to speed up your PC and show a full diagnostics?
Faster surfing with Dish Network High Speed Internet

Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in touch.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com you can find details here Or use the contact link below for further advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.