本文實例講述了JS實現按比例縮放圖片的方法。分享給大家參考,具體如下:
js版本:
function resizeImage(obj, MaxW, MaxH) { var imageObject = obj; var state = imageObject.readyState; if(state!='complete') { setTimeout("resizeImage("+imageObject+","+MaxW+","+MaxH+")",50); return; } var oldImage = new Image(); oldImage.src = imageObject.src; var dW = oldImage.width; var dH = oldImage.height; if(dW>MaxW || dH>MaxH) { a = dW/MaxW; b = dH/MaxH; if( b>a ) a = b; dW = dW/a; dH = dH/a; } if(dW > 0 && dH > 0) { imageObject.width = dW; imageObject.height = dH; } }
使用很簡單:js