第一步:建立一个JS文件,具体内容为var flag=false;function DrawImage(ImgD,imageWidth,imageHeight) {var image=new Image();image.src=ImgD.src;if(image.width>0 && image.height>0){flag=true;if(image.width/image.height>= imageWidth/imageHeight){if(image.width>imageWidth){ImgD.width=imageWidth;ImgD.height=(image.height*imageWidth)/image.width;}else{ImgD.width=image.width;ImgD.height=image.height;}}else{if(image.height>imageHeight){ImgD.height=imageHeight;ImgD.width=(image.width*imageHeight)/image.height;}else{ImgD.width=image.width;ImgD.height=image.height;}}}}
第二步:页面套用1、引入JS文件:<script language="JavaScript" src="img.js" ></script>2、<img src="URL" onload="javascript:DrawImage(this,80,80);" border="0" width="80" height="80" /></img>其中红色的部分就是要等比例缩放的代码