您的位置:首页 > 其它

限制网页图片尺寸

2012-03-29 12:45 330 查看
<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>= 140/180){

if(image.width>140){

ImgD.width=140;

ImgD.height=(image.height*140)/image.width;

}else{

ImgD.width=image.width;

ImgD.height=image.height;

}

/*ImgD.alt=image.width+"×"+image.height;*/

}

else{

if(image.height>180){

ImgD.height=180;

ImgD.width=(image.width*180)/image.height;

}else{

ImgD.width=image.width;

ImgD.height=image.height;

}

/*ImgD.alt=image.width+"×"+image.height;*/

}

}

/*else{

ImgD.src="";

ImgD.alt=""

}*/

}

</script>

把以上这段代码加在<head></head>之间

然后在图片显示的时候,用这种方式:

<img src='xxx.gif' border=0 onload='javascript:DrawImage(this);'> (其中xxx.gif是你要显示的图片)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: