views:

864

answers:

2

Hello!

I've been looking for a really easy/lightweight way to just be able to click thumbnails to switch the src of a larger image.

I haven't actually tried it out yet. Is this the best solution?

The larger images to be swapped in will have the same width but a different height. Will this cause problems/is there something I need to add for this functionality? Would it be better to do a div with a background-image that swaps and is the height of the largest image?

Also, someone said that it only works once (??)... I'd need it to start out on a certain image, then be able to change to 2-4 other images on thumbnail click.

Thanks for any advice! I'm certainly (obviously) no Javascript writer.

+1  A: 

You should be able to switch images as many times as you wish.

The piece of code you reference replaces the image source of #target, with the href of a link within a #thumbs div. It should work fine.

<img id="target" src="images/main.jpg">

<div id="thumbs">
<a href="images/picture1_big.jpg"><img src="images/picture1_small.jpg"></a>
<a href="images/picture2_big.jpg"><img src="images/picture2_small.jpg"></a>
<a href="images/picture3_big.jpg"><img src="images/picture3_small.jpg"></a>
</div>

Now as far as width and height, I am pretty sure there are some cross-browser compatibility issues with how browsers handle a defined width, but an undefined height, when you swap out the images.

In firefox, the following works. Plain old javascript, no jquery:

<html>

   <head>

     <script type="text/javascript">
         function swap(image) {
             document.getElementById("main").src = image.href;
         }
     </script>

   </head>

   <body>

     <img id="main" src="images/main.jpg" width="50">

     <a href="images/picture1_big.jpg" onclick="swap(this); return false;"><img src="images/picture1_small.jpg"></a>
     <a href="images/picture2_big.jpg" onclick="swap(this); return false;"><img src="images/picture2_small.jpg"></a>
     <a href="images/picture3_big.jpg" onclick="swap(this); return false;"><img src="images/picture3_small.jpg"></a>

   </body>
</html>
Jeff B
For the undefined height issue, do you think my idea about using a div background-image swap instead would work better? Thank you for the answer! Also, the jQuery doesn't require any "inline" javascript, correct? So that would be its advantage?
McFly88
Can anyone tell me how to modify the first code to switch a div background-image instead? Would I change the class of the containing div or could I change the background-image directly somehow?
McFly88
Since jQuery is just repackaged javascript, it essentially does the same thing, except that it is assigning the "onclicks" in the code. I.e. $("#thumbs a").click( function() ... assigns click methods to each 'a' tag in the #thumbs div. You can do this with plain javascript too, by using a getElementById and a getElementByTagName and then iterating though the results and then adding event listeners.
Jeff B
To change a background image, you would change the line in the function to read:document.getElementById("main").class.backgroundImage = 'url('+image.href+')';This assumes that main is a div and not an img.
Jeff B
A: 

Would there be a way to implement this with multiple "main" images on one page but all with independence, without having to create a new swap instance for each new "main" image?

Dan Eden