All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
按比例微缩图片的一段小小的JS代码_JavaScript技术_编程技术

Writer: delv Article type: Programming skills(编程技巧) Time: 2013/12/31 6:40:54 Browse times: 338 Comment times: 0

按比例微缩图片的一段小小的JS代码_JavaScript技术_编程技术


Head photo

Go homepage
Upload pictures
Write articles

按比例微缩图片的一段小小的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 就写到这里了,呵呵,是不是很简单啊!






















There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.