shawl.qiu javascript 智能弹出窗口函数 fSmartDisplayImg

来源:互联网 发布:政府办事大厅网络建设 编辑:程序博客网 时间:2024/05/22 10:39

shawl.qiu javascript 智能弹出窗口函数 fSmartDisplayImg


说明:
这个函数主要应用于从缩略图点击时显示大图, 使用弹出窗口友好显示...

如你有 images 文件夹
images 下有目录 thumb, 也就是 images/thumb/

images 用于存放大图片, images/thumb/ 用于存放大图片的缩略图图片

网页显示时, 显示缩略图...添加点击事件后就自动显示大图...

其实我可以有N种方法实现, 不过最终还是选择 js 实现, 这个是在客户端...
从服务端处理这些该死的图片内容要浪费资源和浪费数据表字段...

shawl.qiu
2007-05-02
http://blog.csdn.net/btbtd


内容:
  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.   function fSmartDisplayImg(oImg, sForRemovePath, sCharset, bDebug)
  10.   { // sForRemovePath: /path/
  11.    var Debug = bDebug;
  12.    
  13.    var smallurl = "";
  14.    
  15.    if(typeof(oImg)=="object")
  16.    {
  17.     smallurl = oImg.src;
  18.    }
  19.    else
  20.    {
  21.     smallurl = oImg;
  22.    }
  23.    
  24.    if(typeof(sForRemovePath)=="boolean")
  25.    {
  26.     if(sForRemovePath)
  27.     {
  28.      sForRemovePath = "/thumb/";
  29.     }
  30.     else
  31.     {
  32.      sForRemovePath = "afksjdkfjasdfkjaskdfjaskdfj9089";
  33.     }
  34.    }
  35.    else
  36.    {
  37.     sForRemovePath = sForRemovePath||"/thumb/";
  38.    }
  39.    
  40.    var sCharset = sCharset||"utf-8";
  41.    var re=new RegExp(sForRemovePath, "i");
  42.    var bigurl = smallurl.replace(re, "/");
  43.    var iWidthPlus = 0;
  44.    if(fIsIe())
  45.    {
  46.     iWidthPlus = 16;
  47.    }
  48.    
  49.    var oBigImg = new Image();
  50.    
  51.    with(oBigImg)
  52.    {
  53.     src=bigurl;
  54.    }
  55.    if(oBigImg.width>0)
  56.    {
  57.     fWinPopup(oBigImg.src, oBigImg.width+iWidthPlus, oBigImg.height);
  58.    }
  59.    else
  60.    {  
  61.     oInterval = setInterval
  62.      (
  63.       function()
  64.       {
  65.        if(oBigImg.width>0)
  66.        {
  67.         clearInterval(oInterval);
  68.         fWinPopup(oBigImg.src, oBigImg.width+iWidthPlus, oBigImg.height);
  69.        }
  70.       }
  71.       ,
  72.       50
  73.      )
  74.      
  75.     oBigImg.onerror =
  76.      function()
  77.      {
  78.       alert("failed to loading img: "+oBigImg.src);
  79.      };
  80.    }
  81.    
  82.    if(Debug)
  83.    {
  84.     alert("function fSmartDisplayImg: ok");
  85.     alert("oImg.src: "+oImg.src);
  86.     alert("bigurl: "+bigurl);
  87.     alert("oBigImg.src: "+oBigImg.src);
  88.    }
  89.  
  90.    function fIsIe()
  91.    {
  92.     return navigator.appName=='Microsoft Internet Explorer';
  93.    }
  94.    
  95.    function fWinPopup(sUrl, iWidth, iHeight, sCharset, sAddition){
  96.     var sCharset = sCharset||"utf-8";
  97.     try{oPopup.close()}catch(e){}
  98.     if(!sUrl)return false;
  99.     if(!iWidth)iWidth=screen.availWidth-200;
  100.     if(!iHeight)iHeight=screen.availHeight-150;
  101.     if(!sAddition)sAddition='';
  102.     var iMrgHor=(screen.availWidth-iWidth)/2;
  103.     var iMrgVtc=(screen.availHeight-iHeight)/2;
  104.     
  105.     oPopup=open('about:blank','sqPopup','width='+iWidth+',height='+iHeight+',left='+iMrgHor
  106.      +',top='+iMrgVtc+',scrollbars'+sAddition);
  107.      
  108.      oPopup.document.write(
  109.      "<meta http-equiv=/"Content-Type/" content=/"text/html; charset="+sCharset+"/">"
  110.      );    
  111.      
  112.      oPopup.document.write(
  113.      '<img src="'+sUrl+'"/>',
  114.      '<style>body{margin:0; padding:0;}<//style>'
  115.      );
  116.      
  117.      oPopup.document.close();
  118.  
  119.     oPopup.focus();
  120.     oPopup.document.ondblclick=function(){oPopup.close();}
  121.     oPopup.document.onkeydown=function(){ if(oPopup.event.keyCode==27)oPopup.close(); }
  122.    } // shawl.qiu script 
  123.   } // end function fSmartDisplayImg
  124. //]]>
  125. </script>
  126. </head>
  127. <body>
  128. <img src="images/thumb/246.jpg" onclick="fSmartDisplayImg(this, '/thumb/');" />
  129. </body>
  130. </html>


原创粉丝点击