Home >Web Front-end >JS Tutorial >js control image size core explanation_javascript skills

js control image size core explanation_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:20:46904browse

缩放图片脚本分享

复制代码 代码如下:




New Document





<script> <br><br>function AutoResizeImage(maxWidth,maxHeight){ <br>var objImg = document.getElementById("operImg"); <br>var img = new Image(); <br>img.src = objImg.src; <br>var hRatio; <br>var wRatio; <br>var Ratio = 1; <br>var w = img.width; <br>var h = img.height; <br>wRatio = maxWidth / w; <br>hRatio = maxHeight / h; <br>if (maxWidth ==0 && maxHeight==0){ <br>Ratio = 1; <br>}else if (maxWidth==0){// <br>if (hRatio<1) Ratio = hRatio; <BR>}else if (maxHeight==0){ <BR>if (wRatio<1) Ratio = wRatio; <BR>}else if (wRatio<1 || hRatio<1){ <BR>Ratio = (wRatio<=hRatio?wRatio:hRatio); <BR>} <BR>if (Ratio<1){ <BR>w = w * Ratio; <BR>h = h * Ratio; <BR>} <BR>objImg.height = h; <BR>objImg.width = w; <BR>} <BR></script>





534 X 800









重点js:
复制代码 代码如下:

function AutoResizeImage(maxWidth,maxHeight){
var objImg = document.getElementById("operImg");
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;
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn