js图片等比例缩放

2017/3/1 来源:www.arpun.com 作者:小白

该函数考虑的比较周全

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;

}为你推荐:图片等比例缩放工具 1.0 绿色版 图片等比例缩放

调用方式

<img class='lazy' data-original=www.arpun.com border="0" width="0" height="0" onload="AutoResizeImage(0,0,this)" alt="534 X 800"/>

网友评论
评论(...
全部评论