第一个:
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<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>= 164/112){
if(image.width>164){
ImgD.width=164;
ImgD.height=(image.height*164)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"x"+image.height;
}
else{
if(image.height>112){
ImgD.height=112;
ImgD.width=(image.width*112)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"x"+image.height;
}
}
}
//-->
</script>
</HEAD>
<BODY>
<a href="./img.jpg" target="_blank"><img src="./img.jpg" border="0" width="164" height="112" οnlοad="javascript:DrawImage(this);"></a>
</BODY>
</HTML>
第二个:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/2000/...
<html xmlns="http://www.w3.org/1999/xht...
<head>
<title>图片大小控制</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<body>
此JS脚本实现图片大小自动适应,如果文中有图片宽度超过300,就自动缩小。
在IE、FF里通过测试,但在OPERA需要刷新一次才会自动缩小,
<table id="abc">
<tr>
<td><img src="xxx" />
</tr>
</table>
<script>
var abc=document.getElementById("abc");
var imgs=abc.getElementsByTagName("img");
for (var i=0,g;g=imgs[i];i++)
g.οnlοad=function(){if (this.width>300){this.width=300}else{if (this.height>300)this.height=300}}
</script>
</body>
</html>
用JS控制图片大小按比例放缩图片
最新推荐文章于 2017-05-19 16:32:25 发布