jimingminlovefly

          統計

          最新評論

          js-div遮罩層、div彈出層居中

          思路:打開時,顯示遮罩層ShowMark()和div彈出層showDiv(divObj);關閉時,隱藏遮罩層HideMark()和div彈出層closeDiv(divObj)。

          一、蒙灰層
          //顯示蒙灰層
          function ShowMark(){
               var xp_mark=document.getElementById("xp_mark");
               if(xp_mark!=null) {
                   //設置DIV
                   xp_mark.style.left=0+"px";
                   xp_mark.style.top=0+"px";
                   xp_mark.style.position="absolute";
                   xp_mark.style.backgroundColor="#000";
                   xp_mark.style.zIndex="1";
                   if(document.all) {
                      xp_mark.style.filter="alpha(opacity=50)";
                      var Ie_ver=navigator["appVersion"].substr(22,1);
                      if(Ie_ver==6||Ie_ver==5){hideSelectBoxes();}
                   }
                   else{xp_mark.style.opacity="0.5";}
                   xp_mark.style.width="100%";
          //       var heights=XP_getPageSize().h;
          //       if(heights<600) {
          //         heights=620;
          //       }
          //       xp_mark.style.height=heights+"px";
                   xp_mark.style.height=="100%";
                   xp_mark.style.display="block";
               }
               else{
               //頁面添加div explainDiv,注意必須是緊跟body 內的第一個元素.否則IE6不正常.
               $("body").prepend("<div id='xp_mark' style='display:none;'></div>");
               ShowMark();//繼續回調自己
               } 
                      
          }

          //隱藏蒙灰層
          function HideMark(){
              var xp_mark=document.getElementById("xp_mark");
              xp_mark.style.display="none";   
              var Ie_ver=navigator["appVersion"].substr(22,1);
              if(Ie_ver==6||Ie_ver==5){showSelectBoxes();}
          }

          //獲取頁面的高度與寬度
          function XP_getPageSize(){
              var pt = {w:0,h:0}; 
              if (window.innerHeight && window.scrollMaxY){ 
                pt.w = document.body.scrollWidth;
                pt.h = window.innerHeight + window.scrollMaxY;
              }
              else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
                pt.w = document.body.scrollWidth;
                pt.h = document.body.scrollHeight;
              }
              else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
                pt.w = document.body.offsetWidth;
                pt.h = document.body.offsetHeight;
              }
              return pt;
          }

          //顯示所有的下拉列表框
          function showSelectBoxes(){
           selects = document.getElementsByTagName("select");
           for (i = 0; i != selects.length; i++) {selects[i].style.visibility = "visible"; }
          }

          //隱藏所有的下拉列表框
          function hideSelectBoxes(){
           selects = document.getElementsByTagName("select");
           for (i = 0; i != selects.length; i++) {selects[i].style.visibility = "hidden";}
          }

          二、彈出顯示層div

          //讓層居中顯示-版本

          function setDivToCenter(obj) {
           obj.style.position = "absolute";
           obj.style.zIndex = "222";
           obj.style.display = "block";
           
           var d = document.documentElement, b = document.body, w = window;
           var viewPort ={ left:0, top: 0, width:0, height:0};
           viewPort.top = b.scrollTop || d.scrollTop ; 
           viewPort.left = b.scrollLeft || d.scrollLeft ; 
           viewPort.height = w.innerHeight || d.clientHeight || b.clientHeight; 
           viewPort.width = w.innerWidth || d.clientWidth || b.clientWidth; 

           obj.style.top = (viewPort.top + viewPort.height/2 - obj.offsetHeight/2) + "px";
           obj.style.left = (viewPort.left + viewPort.width/2 - obj.offsetWidth/2) + "px";
          }


          //讓層居中顯示-版本

          function showDiv(obj){//頁面可以用obj == document.getElementById();
           $(obj).show().css({"zIndex":"222","position":"absolute"});
           center(obj);
           $(window).scroll(function(){
            center(obj);
           });
           $(window).resize(function(){
            center(obj);
           });
          }

          function center(obj){//頁面可以用obj == document.getElementById();
              var windowWidth = document.documentElement.clientWidth;  
             var windowHeight = document.documentElement.clientHeight;  
            
             var popupHeight =$(obj).height();  
             var popupWidth =$(obj).width();   
            
             $(obj).css({
              "top": (windowHeight-popupHeight-200)/2+$(document).scrollTop()+130,  
              "left": (windowWidth-popupWidth)/2  
             }); 
          }

           

           //讓層居中隱藏
          function closeDiv(obj){
              $(obj).hide();
              $(window).unbind();
          }

          posted on 2011-12-22 10:03 計明敏 閱讀(7030) 評論(1)  編輯  收藏 所屬分類: js

          評論

          # 1321 2013-07-19 17:22 321312

          額外企鵝王企鵝完全  回復  更多評論   

          主站蜘蛛池模板: 泸溪县| 根河市| 衡南县| 山阴县| 通海县| 中阳县| 通山县| 林芝县| 东乌珠穆沁旗| 赣榆县| 临海市| 鄢陵县| 名山县| 剑阁县| 龙胜| 湟源县| 星座| 巴塘县| 巴东县| 南京市| 和林格尔县| 图们市| 阿拉善盟| 河东区| 怀集县| 沈阳市| 迭部县| 德惠市| 杂多县| 余姚市| 江油市| 英吉沙县| 万宁市| 皮山县| 吴桥县| 慈溪市| 宁波市| 舟山市| 扶沟县| 洛隆县| 蒲城县|