该函数可以定制图片等对象在网页中的显示。因为网页某些位置需要动态加载的不同的图片,而其原本的尺寸、比例又未知,如果设置固定值(如width=50 height=50)又可能显示得变形,所以编写了一个函数以便调用。
picset.js文件
function setobjsize(obj,w,h)
{
var neww,newh,r;
r=w/h;
imgobj = new image();
imgobj.src = obj.href;
if ((imgobj.width !=0) && (imgobj.height !=0))
{
if ((imgobj.width > w || imgobj.height >h)||(imgobj.width < w && imgobj.height <h))
{
if ((imgobj.width)>r*(imgobj.height))
{
obj.height=imgobj.height*w/(imgobj.width);
obj.width=w;
}
else
{
obj.width=imgobj.width*h/(imgobj.height);
obj.height=h;
}
}
else
{
obj.height="100%";
bj.width="100%";
}
}
else
{
settimeout("checkimg('" + theobj + "','"+w+"','"+h+"')", 100);
}
}
调用的网页文件例子
<%
picfile=request("picfile")
%>
<html>
<head>
<title>untitled document</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<script language="javascript" src="picset.js"></script>
</head>
<body>
<table align="center" width="100%" height="100%">
<tr>
<td colspan="2" align="center" height="100%" width="100%" >
<img name="myimg" src="<%=picfile%>" onload="setobjsize(this,100,100)" >
</td>
</tr>
</table>
</body>
</html>