首页 > web前端 > js教程 > js 控制图片大小核心讲解_javascript技巧

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
发布: 2016-05-16 17:20:46
原创
931 人浏览过

缩放图片脚本分享

复制代码 代码如下:




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;
}
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
javascript - js addClass 无效
来自于 1970-01-01 08:00:00
0
0
0
php调用js并获取js的返回值问题
来自于 1970-01-01 08:00:00
0
0
0
javascript - js代码转python
来自于 1970-01-01 08:00:00
0
0
0
找不到js文件代码
来自于 1970-01-01 08:00:00
0
0
0
js高级教程
来自于 1970-01-01 08:00:00
0
0
0
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板