Quote:
Originally Posted by skyhigh007 I've created a list of thumb nails and I want to display a larger image of that tumbnail when someone CLICKS on it. I know you can't do it with css, but how do you do it with javascript ?
I know theres a function called onClickfunction(). Any hints, clues? |
Here's a hint: use the "onClick()" function to change the src location of a target image to be the larger view of the thumbnail. It's really easy, actually.
I'll work up an example in a bit.
EDIT: Ok, here is some sample HTML:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title>gallery</title>
<script type="text/javascript">
function imgSwap(imgid, newimg) {
document.getElementById(imgid).src=newimg;
}
</script>
</head>
<body>
<br>
<br>
<br>
<table
style="width: 500px; text-align: left; margin-left: auto; margin-right: auto;"
border="1" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="text-align: center;"><img
style="width: 100px; height: 75px;" alt="Pic #1"
src="pic1-thumb.jpg"
onclick="imgSwap('large-img','pic1-large.jpg');"></td>
<td style="text-align: center;"><img
style="width: 100px; height: 75px;" alt="Pic #2"
src="pic2-thumb.jpg"
onclick="imgSwap('large-img','pic2-large.jpg');"></td>
<td style="text-align: center;"><img
style="width: 100px; height: 80px;" alt="Pic #3"
src="pic3-thumb.jpg"
onclick="imgSwap('large-img','pic3-large.jpg');"></td>
</tr>
<tr>
<td style="text-align: center;" colspan="5"><img
style="border: 5px ridge red; max-width: 500px;" id="large-img"
alt="" src="filler.gif"></td>
</tr>
</tbody>
</table>
<br>
</body>
</html> Attached are screenshots of the page in action.
Basically, this is what I did. I created a 10x10 transparent GIF called "filler" which is designed to provide a spot for the larger versions of the thumbnails. By NOT specifying the height and width attributes of the filler image, the image area will be automatically resized to the size of the larger image. The 500px max-width I have set is NOT needed and something I did to keep my test images "small" in size.
Now, the JavaScript function, imgSwap, swaps the source of the filler image to be the specified image. In this case, the specified image is the larger version of the thumbnail. Each time someone clicks a thumbnail, the new image is swapped in and sized appropriately.
Here's a step-by-step explanation of the process:
- User loads page. The "filler" GIF is loaded.
- User clicks a thumbnail, imgSwap is called with the id of the "filler" GIF and the filename of the image to be displayed (i.e. the large version of the thumbnail). The new image appears.
- User clicks another thumbnail and step #2 repeats.
This is a working "proof of concept" of what it sounds like you're looking for. Good luck!
EDIT #2: Using a HTML table for this isn't necessary. The same kind of thing can be done using DIVs or some combination of DIVs and tables, etc.
Peace...