Int
07-29-2009, 02:38 AM
I'm currently creating a portfolio website for a friend of mine, so they can display and sell their artwork. There is a grid of thumbnails on the right, and when one of the thumbnails is clicked, a larger version of the image appears in the center of the page.
Some of their illustrations are quite large, so I've been trying to write a script that will resize them to a maximum width and/or height of 500px when they appear as a larger version, but for some reason only the image that is first clicked will resize the central image correctly.
Here's my code:
function dispImage(newSrc)
{
if (document.images['enlargedImage'].src == '')
{
document.images['enlargedImage'].src=newSrc;
}
var oldHeight = document.images['enlargedImage'].height;
var oldWidth = document.images['enlargedImage'].width;
var aspectRatio = oldHeight/oldWidth;
if (500/aspectRatio < 500)
{
newHeight = 500;
newWidth = 500 / aspectRatio;
}
else
{
newHeight = 500 * aspectRatio;
newWidth = 500;
}
document.images['enlargedImage'].src = newSrc;
document.images['enlargedImage'].height = newHeight;
document.images['enlargedImage'].width = newWidth;
}
enlargedImage refers to the image in which the large version of the thumbnail appears.
Example HTML of one of the thumbnails:
<img src="images/uploads/thumb_1248826052.jpg" onClick="dispImage('images/uploads/l_1248826052.jpg')">
So the first image that is clicked on will resize enlargedImage correctly. But if any other images are clicked after that, enlargedImage will not resize.
I have a feeling I'm going about this the wrong way, but my experience with Javascript is not that much so I'm not sure.
Some of their illustrations are quite large, so I've been trying to write a script that will resize them to a maximum width and/or height of 500px when they appear as a larger version, but for some reason only the image that is first clicked will resize the central image correctly.
Here's my code:
function dispImage(newSrc)
{
if (document.images['enlargedImage'].src == '')
{
document.images['enlargedImage'].src=newSrc;
}
var oldHeight = document.images['enlargedImage'].height;
var oldWidth = document.images['enlargedImage'].width;
var aspectRatio = oldHeight/oldWidth;
if (500/aspectRatio < 500)
{
newHeight = 500;
newWidth = 500 / aspectRatio;
}
else
{
newHeight = 500 * aspectRatio;
newWidth = 500;
}
document.images['enlargedImage'].src = newSrc;
document.images['enlargedImage'].height = newHeight;
document.images['enlargedImage'].width = newWidth;
}
enlargedImage refers to the image in which the large version of the thumbnail appears.
Example HTML of one of the thumbnails:
<img src="images/uploads/thumb_1248826052.jpg" onClick="dispImage('images/uploads/l_1248826052.jpg')">
So the first image that is clicked on will resize enlargedImage correctly. But if any other images are clicked after that, enlargedImage will not resize.
I have a feeling I'm going about this the wrong way, but my experience with Javascript is not that much so I'm not sure.