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

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


说明: 
不想重复描述相同的事, 第一版链接及其详细说明在这:
http://blog.csdn.net/btbtd/archive/2006/10/27/1353481.aspx

本版实现了完全 兼容 IE, Opera, Firefox 的显示, 及优化相关操作代码. 

shawl.qiu 
2006-11-16
http://blog.csdn.net/btbtd

函数 fResizeImg(w, h, id) 源码及使用演示:
  1. linenum
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns=" http://www.w3.org/1999/xhtml">
  4. <!-- DW6 -->
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <title>shawl.qiu template</title>
  8. <script type="text/javascript">
  9. //<![CDATA[
  10.     window.οnlοad=function(){
  11.         fResizeImg(400, 300, 'table');
  12.         //fResizeImg(500, 500);
  13.     }
  14.     /*------------------------------------------------------*/
  15.      * Subject: 使用 javascript 函数 完美控制页面图片显示大小 By shawl.qiu  
  16.      * 使用:
  17.      * --------------------------------------
  18.      * 1. Javascript 脚本块
  19.      *    window.οnlοad=function(){
  20.      *        fResizeImg(500, 500, 'img');
  21.      *        //fResizeImg(500, 500);
  22.      *    }
  23.      *
  24.      * 2. html body 标签
  25.      * <body οnlοad="fResizeImg(500, 500, 'textMain');" >
  26.      * --------------------------------------
  27.      * 注: 必须在页面加载完毕后使用本函数(这是基本的客户端应用概念)
  28.      * --------------------------------------
  29.      * 参数说明:
  30.      * w: 数值, 设置最大宽度, 图片宽度大于该值则设置大小为该值
  31.      * h: 数值, 设置最大高度, 图片高度大于该值则设置大小为该值
  32.      * id: 字串, 设置函数作用域 ID, 如不设置, 作用于全页面. 
  33.     /*-------------------------------------------------------*/
  34.     //-----------------------------begin function fResizeImg(w, h, id)-------------------------//
  35.     function fResizeImg(w, h, id){
  36.         var img, obj;
  37.         if(!id)obj=document.images;
  38.         else obj=document.getElementById(id).getElementsByTagName('img');
  39.         
  40.         for(var i=0; i<obj.length; i++){
  41.             img=obj[i];
  42.             if(img.width>w&&(img.height<img.width)){
  43.                 img.height=img.height-(img.height/(img.width/(img.width-w)))
  44.                 img.width=w;
  45.             }else if(img.height>h&&(img.height>img.width)){
  46.                 img.width=img.width-(img.width/(img.height/(img.height-h)))
  47.                 img.height=h;
  48.             }
  49.             img.οnclick=function(){
  50.                 try{ w.close();} catch(e){}
  51.                 var temp=new Image();
  52.                     temp.src=this.src;
  53.                 var wW=temp.width;
  54.                 var wH=temp.height;
  55.                 w=open('#', 'imgurl', 'width='+wW+', height='+wH+
  56.                 ', left='+(screen.availWidth-wW)/2+', top='+(screen.availHeight-wH)/2);
  57.                 w.document.write('<script>document.οnclick=function(){ close();}<//script>');
  58.                 w.document.write('<img src="'+this.src+'"/>',
  59.                 '<style>body{margin:0; padding:0;} .hd{visibility:hidden;}<//style>');
  60.                 w.focus();
  61.                 w.document.close();
  62.                 return true;
  63.             }
  64.         } // shawl.qiu script
  65.         return true;
  66.     }
  67.     //-----------------------------end function fResizeImg(w, h, id)---------------------------//
  68. //]]>
  69. </script>
  70. </head>
  71. <body>
  72. <table width="100%"  border="1" id="table">
  73.   <tr>
  74.     <td><img src="temp/2006-10-15/01.jpg" width="400" height="381" /></td>
  75.     <td><img src="mod/system/themes/default/default/images/gi_logo_88_31.png" width="88" height="31" /></td>
  76.     <td><img src="mod/system/themes/default/default/images/gi_logo_88_31.png" width="88" height="31" /></td>
  77.   </tr>
  78.   <tr>
  79.     <td><img src="mod/managemain/themes/default/default/images/gi_logo_300_92.png" width="300" height="92" /></td>
  80.     <td><img src="images/03_1.jpg" width="832" height="1126" /></td>
  81.     <td><img src="images/gi_logo_300_92.png" width="300" height="92" /></td>
  82.   </tr>
  83.   <tr>
  84.     <td><img src="images/04.jpg" width="640" height="467" /></td>
  85.     <td>&nbsp;</td>
  86.     <td>&nbsp;</td>
  87.   </tr>
  88. </table>
  89. </body>
  90. </html>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值