Simple jQuery Gallery

Posted on July 16, 2009 by Ashley

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

This entry was posted in Downloads, Javascript, Tutorials, Web Tools, jQuery and tagged , , , , . Bookmark the permalink.
Comments
24 discussions around Simple jQuery Gallery
  1. Pingback: CSS Brigit | Create a simple gallery with jQuery

  2. Jake says:

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

  3. Ashley says:

    @jake here’s the link http://papermashup.com/demos/jquery-gallery/ click the thumbnails under the image to switch the image. You may have been confused by the fact that I’m using screenshots of other sites in the gallery. Let me know if you have any probs :)

  4. 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

  5. Evulgo Media says:

    WAW!
    Just as i needed.
    Thank a lot!

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

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

  8. 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?

  9. 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.

  10. Ashley says:

    @Nathaniel your on the wrong post :) I wrote a new post with the title and description over here: http://papermashup.com/jquery-gallery-update-with-title-and-description/

  11. Jose luis Rivero says:

    Very nice website

  12. 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!!

  13. Ashley says:

    @Alfred, good suggestion. I’ll look to update the post when i get some time

  14. Alfred says:

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

    = ] Thanks again!

  15. 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

  16. 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?

  17. Iarrieta says:

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

    Sorry

  18. Ashley says:

    @Iarrieta, no problem glad to hear you got it sorted ;)

  19. Gary says:

    Gracias, me fue de mucha utilidad!

  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. I’ve been looking for something nice and simple like this. Thanks for sharing

  22. 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

  23. 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.

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>

Subscribers
1,250
Twitter
510
Comments
1,207
Posts
125