Heim > Web-Frontend > js-Tutorial > js 控制图片大小核心讲解_javascript技巧

js 控制图片大小核心讲解_javascript技巧

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Freigeben: 2016-05-16 17:20:46
Original
937 Leute haben es durchsucht

缩放图片脚本分享

复制代码 代码如下:




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}else if (maxHeight==0){
if (wRatio}else if (wRatioRatio = (wRatio}
if (Ratiow = w * Ratio;
h = h * Ratio;
}
objImg.height = h;
objImg.width = w;
}
Verwandte Etiketten:
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Aktuelle Ausgaben
Wo steht js?
Aus 1970-01-01 08:00:00
0
0
0
js-Dateicode nicht gefunden
Aus 1970-01-01 08:00:00
0
0
0
js addClass funktioniert nicht
Aus 1970-01-01 08:00:00
0
0
0
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage