js 控制图片大小核心讲解_javascript技巧
程序员文章站
2022-05-15 10:52:59
...
缩放图片脚本分享
New Document
重点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;
}
复制代码 代码如下:
重点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;
}
推荐阅读
-
JS 控制CSS样式表_javascript技巧
-
js控制滚动条缓慢滚动到顶部实现代码_javascript技巧
-
IE下通过JS控制剪贴板的代码_javascript技巧
-
js实现DIV的一些简单控制_javascript技巧
-
js控制iframe的高度/宽度让其自适应内容_javascript技巧
-
js控制不同的时间段显示不同的css样式的实例代码_javascript技巧
-
js 上下左右键控制焦点(示例代码)_javascript技巧
-
IE下通过JS控制剪贴板的代码_javascript技巧
-
js实现DIV的一些简单控制_javascript技巧
-
js鼠标及对象坐标控制属性详细解析_javascript技巧