zoukankan
html css js c++ java
按比例微缩图片的一段小小的JS代码
1
1
//
智能微缩图片JS方法
2
2
//
参数:imgID(图片的标识ID)
3
3
//
参数:maxWidth(图片的最大宽度,值为0则表示不限制宽度)
4
4
//
参数:maxHeight(图片的最大高度,值为0则表示不限制高度)
5
5
function setImgSize(imgID,maxWidth,maxHeight)
6
6
{
7
7
var img
=
document.images[imgID];
8
8
if
(maxWidth
<
1
)
9
9
{
10
10
if
(img.height
>
maxHeight)
11
11
{
12
12
img.height
=
maxHeight;
13
13
}
14
14
return
true
;
15
15
}
16
16
if
(maxHeight
<
1
)
17
17
{
18
18
if
(img.width
>
maxWidth)
19
19
{
20
20
img.width
=
maxWidth;
21
21
}
22
22
return
true
;
23
23
}
24
24
if
(img.height
>
maxHeight
||
img.width
>
maxWidth)
25
25
{
26
26
if
((img.height
/
maxHeight)
>
(img.width
/
maxWidth))
27
27
{
28
28
img.height
=
maxHeight;
29
29
}
30
30
else
31
31
{
32
32
img.width
=
maxWidth;
33
33
}
34
34
return
true
;
35
35
}
36
36
}
自己写的一个非常简单的图片微缩JS代码,当然网上有很多类似的代码,在此确实是献丑了。
主要方法写在SetImgSize.js里面
功能实现原理是在图片加载完毕后(onload事件)用JS实现微缩。
下面是一个测试用的文件test.htm
1
1
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
2
2
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
3
3
<
head
>
4
4
<
title
>
测试
</
title
>
5
5
<
script
language
="javascript"
type
="text/javascript"
src
="setImgSize.js"
></
script
>
6
6
</
head
>
7
7
<
body
>
8
8 限定了宽度150px
9
9
<
br
/><
img
alt
=""
id
="img1"
onload
="setImgSize('img1',150,0);"
src
="http://img.poco.cn/photo/20060602/972374149620060602140117_1.jpg"
/>
10
10
<
br
/>
限定了高度150px
11
11
<
br
/><
img
alt
=""
id
="img2"
onload
="setImgSize('img2',0,150);"
src
="http://img.poco.cn/photo/20060602/972374149620060602140117_4.jpg"
/>
12
12
<
br
/>
限定了高度150px、宽度150px
13
13
<
br
/><
img
alt
=""
id
="img3"
onload
="setImgSize('img3',150,150);"
src
="http://static.flickr.com/46/147572720_8b25471150_o.jpg"
/>
14
14
</
body
>
15
15
</
html
>
就写到这里了,呵呵,是不是很简单啊!
下载
SetImgSize.rar
http://u.huoban001.com/space.php
查看全文
相关阅读:
iosopendev配置
按Home键切换到后台后会触发libGPUSupportMercury.dylib: gpus_ReturnNotPermittedKillClient导致crash
iphone图片简单处理
iPhone开发小工具
iphone开发设置默认字体
NSString+TimeCategory
UIButton zoomin pressed
Centos7下卸载docker
如何清理Docker占用的磁盘空间
美国VPS推荐1GB 50GB可以win
原文地址:https://www.cnblogs.com/zpq521/p/817547.html
最新文章
css+js自动化开发之第十五天
前端html+css之第十四天
python自动开发之第十三天
python自动开发之第十二天
python运维开发之第十一天(RabbitMQ,redis)
python运维开发之第十天
python运维开发之第九天
python运维开发之第八天(socket)
我的Python成长之路---第二天---Python基础(8)---2016年1月9日(晴)
我的Python成长之路---第二天---Python基础(7)---2016年1月9日(晴)
热门文章
我的Python成长之路---第一天---Python基础(作业2:三级菜单)---2015年12月26日(雾霾)
我的Python成长之路---第一天---Python基础(作业1:登录验证)---2015年12月26日(雾霾)
我的Python成长之路---第一天---Python基础(6)---2015年12月26日(雾霾)
我的Python成长之路---第一天---Python基础(5)---2015年12月26日(雾霾)
我的Python成长之路---第一天---Python基础(4)---2015年12月26日(雾霾)
我的Python成长之路---第一天---Python基础(3)---2015年12月26日(雾霾)
我的Python成长之路---第一天---Python基础(1)---2015年12月26日(雾霾)
限制UITextField输入长度
search bar 自定义背景
lua_to_luac
Copyright © 2011-2022 走看看