Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 网站建设 > ASP.NET > 使用 javascript 函数 完美控制页面图片显示大小(第二版) By shawl.qiu
【标  题】:使用 javascript 函数 完美控制页面图片显示大小(第二版) By shawl.qiu
【关键字】:javascript,By,shawl.qiu
【来  源】:http://blog.csdn.net/btbtd/archive/2006/11/16/1388764.aspx

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

Your Ad Here

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


ie下ajax使用的一点小bug:【上一篇】
Perl Tips(1):【下一篇】
【相关文章】
  • XMPP协议之Socket5 Bytestream文件传输
  • 浅谈Ruby on Rails - Rails 的 MVC
  • 浅谈Ruby on Rails - Rails 在服务器上的部署
  • Javascript元素拖曳操作Byshawl.qiu (兼容IE,Opera,Firefox)
  • 实用javaScript技术总结(2):表单提交验证类
  • 用脚本(JavaScript)去掉字符串的前后空格
  • 深入Atlas系列:Web Sevices Access in Atlas示例(7) - 编写JavaScriptConverter处理含有循环引用的类型
  • 用javascript多文件上传
  • A Class Capturing Video by DShow
  • 使用 javascript HTML DOM 高亮显示页面特定字词 By shawl.qiu
  • 【随机文章】
  • Javascript实例教程(5) 利用Javascript创建对象
  • .net一些术语解释
  • HFSoft.Data2.0数据持久组件性能简要测试结果
  • 策略模式
  • XP WAV 文件处理拒绝服务漏洞
  • 用XML创建可排序、分页的数据显示页面
  • 在启动Windows CE时,隐藏“开始”菜单,并全屏显示Internet Explorer程序
  • 一段关于memcpy的代码
  • shell输入与输出
  • JDBC连SQL SERVER数据库常见问题
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.