<script type="text/JavaScript">
//<![CDATA[
window.onload=function(){
fResizeImg(500, 500, 'img');
//fResizeImg(500, 500);
}
/*------------------------------------------------------*\
* Subject: 使用 JavaScript 函数 完美控制页面图片显示大小 By shawl.qiu
* 使用:
* --------------------------------------
* 1. JavaScript 脚本块
* window.onload=function(){
* fResizeImg(500, 500, 'img');
* //fResizeImg(500, 500);
* }
*
* 2. HTML body 标签
* <body onload="fResizeImg(500, 500, 'textMain');" >
* --------------------------------------
* 注: 必须在页面加载完毕后使用本函数(这是基本的客户端应用概念)
\*-------------------------------------------------------*/
//---------------------------------begin function fResizeImg();
function fResizeImg(w, h, id){
var img='';
var obj;
if(id==undefined)obj=document.images;
else obj=document.getElementById(id).getElementsByTagName('img');
for(var i=0; i<obj.length; i++){
img=obj[i];
if(img.width>w&&(img.height<img.width)){
img.height=img.height-(img.height/(img.width/(img.width-w)))
img.width=w;
}else if(img.height>h&&(img.height>img.width)){
img.width=img.width-(img.width/(img.height/(img.height-h)))
img.height=h;
}
img.onclick=function(){
try{ imgPopup.close();} catch(e){}
imgPopup=open('#', 'imgurl', 'width=500, height=500, left='+(screen.availWidth-500)/2+
', top='+(screen.availHeight-500)/2')
imgPopup.document.write('<script>document.onclick=function(){ close();} /* 单击关闭窗口 */ <\/script>');
imgPopup.document.write('<img src="'+this.src+'"/>'+
'<script>'+
'var w, h;'+
'var img=document.images[0];'+
'if(navigator.appName=="Opera"){w=img.width+10; h=img.height+40} else {w=img.width+10; h=img.height+25};'+
'self.resizeTo(w, h);'+
'self.moveTo((screen.availWidth-img.width)/2,(screen.availHeight-img.height)/2)'+
'<\/script>'+
'<style>body{margin:0; padding:0;} .hd{visibility:hidden;}<\/style>');
imgPopup.document.write('<p class="hd">ok</p>');
imgPopup.document.close();
imgPopup.focus();
}
} // shawl.qiu script
}
//---------------------------------end function fResizeImg();
//]]>
</script>

BK网络学院主要内容:平面设计教程,网站开发在线教程,网页制作教程,服务器教程,网络编程,数据库教程等。