justAnoob Posted January 5, 2010 Share Posted January 5, 2010 I found this little script online What I'm looking to do is associate this function with a div that I have. Why, well, I have thumbnails that the user can rollover and the full size image is displayed in that div. That's great and all but when there are pictures that are in a portrait format, the pic shows but it throws the rest of my page off wack due to the height. So I'm looking to mouse over the thumbnails and have a max width and height of 380 while keeping the original aspect ratio of the picture. (I'm sure I could set a max height/width on the upload script, but this way seems easier) Can this be done. function ChangePhoto(newPhoto) { img = document.getElementById('swapme'); img.src='images/'+newPhoto; var width = img.naturalWidth; var height = img.naturalHeight; var newx = 350; // set to whatever you want the images max width to be. var newy = 350; // whatever you want the images maximum height to be. if ( width >= height ) { // landscape var tmpy = height*newx/width; if ( tmpy <= newy ) newy = tmpy; else newx = width*newy/height; } else { // portrait tmpx = width*newy/height; if ( tmpx <= newx ) newx = tmpx; else newy = height*newx/width; } img.width = newx; img.height = newy; } Quote Link to comment Share on other sites More sharing options...
justAnoob Posted January 5, 2010 Author Share Posted January 5, 2010 Decided to go with the upload script change. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.