Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)delv
published in(发表于) 2013/12/31 6:40:54
按比例微缩图片的一段小小的JS代码_JavaScript技术_编程技术

按比例微缩图片的一段小小的JS代码_JavaScript技术_编程技术-你的首页-uuhomepage.com





自己写的一个非常简单的图片微缩JS代码,当然网上有很多类似的代码,在此确实是献丑了。
主要方法写在SetImgSize.js里面 SetImgSize.js
1 //智能微缩图片JS方法
2 //参数:imgID(图片的标识ID)
3 //参数:maxWidth(图片的最大宽度,值为0则表示不限制宽度)
4 //参数:maxHeight(图片的最大高度,值为0则表示不限制高度)
5 function setImgSize(imgID,maxWidth,maxHeight)
6 {
7 var img = document.images[imgID];
8 if(maxWidth < 1)
9 {
10 if(img.height > maxHeight)
11 {
12 img.height = maxHeight;
13 }
14 return true;
15 }
16 if(maxHeight < 1)
17 {
18 if(img.width > maxWidth)
19 {
20 img.width = maxWidth;
21 }
22 return true;
23 }
24 if(img.height > maxHeight || img.width > maxWidth)
25 {
26 if((img.height / maxHeight) > (img.width / maxWidth))
27 {
28 img.height = maxHeight;
29 }
30 else
31 {
32 img.width = maxWidth;
33 }
34 return true;
35 }
36 } 功能实现原理是在图片加载完毕后(onload事件)用JS实现微缩。
下面是一个测试用的文件test.htm test.htm
1 http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 http://www.w3.org/1999/xhtml" >
3
4 测试
5
6
7
8 限定了宽度150px
9
http://img.poco.cn/photo/20060602/972374149620060602140117_1.jpg" />
10
限定了高度150px
11
http://img.poco.cn/photo/20060602/972374149620060602140117_4.jpg" />
12
限定了高度150px、宽度150px
13
http://static.flickr.com/46/147572720_8b25471150_o.jpg" />
14
15 就写到这里了,呵呵,是不是很简单啊!









If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759