This article describes the same-scale scaling method of JS images. Share it for your reference, as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>javascript automatically displays pictures in proportion, compresses pictures in proportion</title><script type="text/javascript">function AutoResizeImage(maxWidth,maxHeight,objImg){var img = new Image();img.src = objImg.src;var hRatio;var wRatio;var Ratio = 1;var w = img.width;var h = img.height;wRatio = maxWidth / w;hRatio = maxHeight / h;if (maxWidth ==0 && maxHeight==0){Ratio = 1;}else if (maxWidth==0){//if (hRatio<1) Ratio = hRatio;}else if (maxHeight==0){if (wRatio<1) Ratio = wRatio;}else if (wRatio<1 || hRatio<1){Ratio = (wRatio<=hRatio?wRatio:hRatio);}if (Ratio<1){w = w * Ratio;h = h * Ratio;}objImg.height = h;objImg.width = w;}</script></head><body><br /><img src="1.jpg" onload="AutoResizeImage(100,0,this)" />width:100px<br /><br /><img src="1.jpg" onload="AutoResizeImage(0,100,this)" />height:100px<br /><br /><img src="1.jpg" onload="AutoResizeImage(100,100,this)" />width:100px height:100px<br /><br /><br /><br /><br /><br /><br /><br /></body></html>For more information about JavaScript related content, please check out the topics of this site: "Summary of JavaScript switching effects and techniques", "Summary of JavaScript search algorithm skills", "Summary of JavaScript animation effects and techniques", "Summary of JavaScript errors and debugging techniques", "Summary of JavaScript data structures and algorithm skills", "Summary of JavaScript traversal algorithms and techniques", and "Summary of JavaScript mathematical operations usage"
I hope this article will be helpful to everyone's JavaScript programming.