用javascript自动缩小超出大小的图片
上传人:Mykame 回复/点击:0/120 日期:2010-6-28 11:34:04
推荐: 分类:【程序语言】 原贴地址:http://www.mykame.com/blog/
推荐: 分类:【程序语言】 原贴地址:http://www.mykame.com/blog/
<script language="JavaScript">
<!--
var flag=false;
function DrawImage(ImgD){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= 200/200){
if(image.width>200){
ImgD.width=200;
ImgD.height=(image.height*200)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
else{
if(image.height>200){
ImgD.height=200;
ImgD.width=(image.width*200)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
}
}
//-->
</script>
把上边的这段代码加入<head></head>之中
在相关图片上加入以下代码或者把onload的代码加入到body里:
<img src=# onload=javascript:DrawImage(this); >
<!--
var flag=false;
function DrawImage(ImgD){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= 200/200){
if(image.width>200){
ImgD.width=200;
ImgD.height=(image.height*200)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
else{
if(image.height>200){
ImgD.height=200;
ImgD.width=(image.width*200)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
}
}
//-->
</script>
把上边的这段代码加入<head></head>之中
在相关图片上加入以下代码或者把onload的代码加入到body里:
<img src=# onload=javascript:DrawImage(this); >
上一篇:javascript下拉菜单
下一篇:CSS在线编辑器发布
