本文实例讲述了JS图片等比例缩放方法。分享给大家供大家参考,具体如下:
/p>
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
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;
}
border="0"
width="0"
height="0"
οnlοad="AutoResizeImage(100,0,this)"
/>
width:100px
border="0"
width="0"
height="0"
οnlοad="AutoResizeImage(0,100,this)"
/>
height:100px
border="0"
width="0"
height="0"
οnlοad="AutoResizeImage(100,100,this)"
/>
width:100px height:100px
希望本文所述对大家JavaScript程序设计有所帮助。