Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best jQuery "click a thumbnail and change the main image" module?

Here's what I have (all generated dynamically, if that makes a difference) :

  • A list of images
  • A caption for each image
  • A thumbnail for each image

The page should load with one full-size image and all the thumbnails. When a user clicks a thumbnail, the full-size image shows that new image with its caption. If they click another thumbnail, the picture (and caption) change again.

It's not very complex. I hacked together a solution a few months ago, but I need to do it again and I'm looking at this crappy code and thinking that there has to be a better way (and knowing jQuery, someone else has already done it, and done it well).

Thoughts? Links?

Thanks!

like image 485
Eileen Avatar asked Mar 04 '09 21:03

Eileen


6 Answers

Most of the answers here are like shooting a fly with a canon !!

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

this is all you need .. 4 lines of code .

look here : gallery-in-4-lines

like image 152
krembo99 Avatar answered Nov 11 '22 09:11

krembo99


Have you tried Lightbox? http://leandrovieira.com/projects/jquery/lightbox/

like image 3
Donnie Thomas Avatar answered Nov 11 '22 08:11

Donnie Thomas


Here's one that looks pretty nice and is written in jQuery: Photo Slider

And here is another which I like a bit better: Galleria

like image 2
Allie the Icon Avatar answered Nov 11 '22 08:11

Allie the Icon


Building off of krembo99's answer he linked here, I wanted to share my solution as I had already uploaded hundreds of photos when my client had requested a feature like this. With that in mind, by adding a few extra lines to the provided code, I was able to get a solution that fit my parameters.

I was also working with smaller images as well, so I had no need to go through creating small & large versions of the same file.

$('.thumbnails img').click(function(){

 // Grab img.thumb class src attribute
 // NOTE: ALL img tags must have use this class, 
 // otherwise you can't click back to the first image.

 var thumbSrc = $('.thumb').attr('src');

 // Grab img#largeImage src attribute
 var largeSrc = $('#largeImage').attr('src');

  // Use variables to replace src instead of relying on file names.
  $('#largeImage').attr('src',$(this).attr('src').replace(thumbSrc,largeSrc));
  $('#description').html($(this).attr('alt'));

});

Here's how my HTML looks.

    <img src="path/to/file1.jpg" id="largeImage" class="thumb">
    <div id="description">1st image Description</div>

    <div class="thumbnails">

     <img src="path/to/file1.jpg" class="thumb" alt="1st image description">
     <img src="path/to/file2.jpg" class="thumb"alt="2nd image description">
     <img src="path/to/file3.jpg" class="thumb" alt="3rd image description">
     <img src="path/to/file4.jpg" class="thumb" alt="4th image description">
     <img src="path/to/file5.jpg" class="thumb" alt="5th image description">

    </div>
like image 2
M. Cotter Avatar answered Nov 11 '22 09:11

M. Cotter


http://bradblogging.com/jquery/9-jquery-slideshow-applications-you-cannot-miss/

A page with 9 different photo slideshows in jQuery ready to use

like image 1
Xunil Avatar answered Nov 11 '22 09:11

Xunil


Check out my galleria implementation: Garden design Landscaper in Essex Gallery

like image 1
Kenco Gold Avatar answered Nov 11 '22 08:11

Kenco Gold