js按比例缩小图片

来源:互联网 发布:安卓刷机软件哪个好 编辑:程序博客网 时间:2024/05/16 05:55
function loadimg(maxWidth,maxHeight,objImg){        var img = new Image();        img.src = objImg.src;var hRatio;var wRatio;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;}


<img onload="loadimg(800,500,this)" id="target" src="" />