該函數可以定制圖片等對象在網頁中的顯示。因為網頁某些位置需要動態加載的不同的圖片,而其原本的尺寸、比例又未知,如果設置固定值(如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>
</body> </html>
|