使用 javascript 函数 完美控制页面图片显示大小 By shawl.qiu

使用 javascript 函数 完美控制页面图片显示大小 By shawl.qiu 


函数 fResizeImg(w, h, id) 功能说明:
1. 本函数实现了指定高宽度显示图片大小
2. 本函数实现了按比例显示指定的高宽度
3. 本函数实现了是否指定 id 进行操作, 若不指定 id, 默认为页面全部图片.
4. 本函数实现了自动添加单击事件
4.1 单击图片后弹出一个窗口显示图片
4.2 再单击弹出的窗口则关闭本窗口
5. 弹出窗口按图片大小居中显示
6. 完美支持 IE, Opera 浏览器, 但对 Firefox 支持不够完善(由于个人习惯, 鄙人不打算费精力去完善对 Firefox 的支持)

目录:
1. fResizeImg(w, h, id) 函数源码及调用演示

shawl.qiu 
2006-10-27
http://blog.csdn.net/btbtd

  1. linenum
  2. <script type="text/javascript">
  3. //<![CDATA[
  4. window.οnlοad=function(){
  5.     fResizeImg(500, 500, 'img');
  6.     //fResizeImg(500, 500);
  7. }
  8.     /*------------------------------------------------------*/
  9.      * Subject: 使用 javascript 函数 完美控制页面图片显示大小 By shawl.qiu  
  10.      * 使用:
  11.      * --------------------------------------
  12.      * 1. Javascript 脚本块
  13.      *    window.οnlοad=function(){
  14.      *        fResizeImg(500, 500, 'img');
  15.      *        //fResizeImg(500, 500);
  16.      *    }
  17.      *
  18.      * 2. html body 标签
  19.      * <body οnlοad="fResizeImg(500, 500, 'textMain');" >
  20.      * --------------------------------------
  21.      * 注: 必须在页面加载完毕后使用本函数(这是基本的客户端应用概念)
  22.     /*-------------------------------------------------------*/
  23.     //---------------------------------begin function fResizeImg();
  24.     function fResizeImg(w, h, id){
  25.         var img='';
  26.         var obj;
  27.         if(id==undefined)obj=document.images;
  28.         else obj=document.getElementById(id).getElementsByTagName('img');
  29.         
  30.         for(var i=0; i<obj.length; i++){
  31.             img=obj[i];
  32.             if(img.width>w&&(img.height<img.width)){
  33.                 img.height=img.height-(img.height/(img.width/(img.width-w)))
  34.                 img.width=w;
  35.             }else if(img.height>h&&(img.height>img.width)){
  36.                 img.width=img.width-(img.width/(img.height/(img.height-h)))
  37.                 img.height=h;
  38.             }
  39.             
  40.             img.οnclick=function(){
  41.                 try{ imgPopup.close();} catch(e){}
  42.                 imgPopup=open('#', 'imgurl', 'width=500, height=500, left='+(screen.availWidth-500)/2+
  43.                 ', top='+(screen.availHeight-500)/2)
  44.                 imgPopup.document.write('<script>document.οnclick=function(){ close();} /* 单击关闭窗口 */ <//script>');
  45.                 
  46.                 imgPopup.document.write('<img src="'+this.src+'"/>'+
  47.                     '<script>'+
  48.                         'var w, h;'+
  49.                         'var img=document.images[0];'+
  50.                         'if(navigator.appName=="Opera"){w=img.width+10; h=img.height+40} else {w=img.width+10; h=img.height+25};'+
  51.                         'self.resizeTo(w, h);'+
  52.                         'self.moveTo((screen.availWidth-img.width)/2,(screen.availHeight-img.height)/2)'+
  53.                     '<//script>'+
  54.                     '<style>body{margin:0; padding:0;} .hd{visibility:hidden;}<//style>');
  55.                 imgPopup.document.write('<p class="hd">ok</p>');
  56.                 imgPopup.document.close();
  57.                 imgPopup.focus();
  58.             }
  59.         } // shawl.qiu script
  60.     }
  61.     //---------------------------------end function fResizeImg();
  62. //]]>
  63. </script>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值