Does anyone know how to resize images proportionally using JavaScript?
I have tried to modify the DOM by adding attributes height
and width
on the fly, but seems did not work on IE6.
Answer: Use the JavaScript width and height property You can use either width or height JavaScript property to proportionally increase and decrease the dimension of an image like zoom-in and zoom-out feature.
One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels.
Right-click on the image and select Resize Image.... After selecting Resize Image... a dialog will appear that gives you precise control over the size of the image.
To modify an image proportionally, simply only alter one of the width/height css properties, leave the other set to auto.
image.style.width = '50%' image.style.height = 'auto'
This will ensure that its aspect ratio remains the same.
Bear in mind that browsers tend to suck at resizing images nicely - you'll probably find that your resized image looks horrible.
okay it solved, here is my final code
if($(this).width() > $(this).height()) { $(this).css('width',MaxPreviewDimension+'px'); $(this).css('height','auto'); } else { $(this).css('height',MaxPreviewDimension+'px'); $(this).css('width','auto'); }
Thanks guys
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With