海水正藍

          面朝大海,春暖花開
          posts - 145, comments - 29, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          需求場景:

          1.  頁面內有多級iframe嵌套。

          2.  iframe內部某些按鈕點擊后需要彈出浮層。

          3.  浮層需要將整個瀏覽器窗口遮罩,且浮層位于瀏覽窗口中部。

           

          效果如下: 

           

          解決思路:

          1. 頂層頁面內預留用于顯示浮層的div(命名為popdiv),且該div內有一預留的iframe,該iframe用于加載浮層內容,命名為popiframe
          2. 提供可以直接訪問浮層內容的url連接
          3. iframe觸發顯示浮層事件時,通過window.top 設置頂層窗口的popiframe.src。
          4. 通過計算浮層內容的長寬及當前窗口的長寬設置popdiv的位置,使其在窗口中央顯示。

           

           

           

          實現:

          頂層頁面相關html代碼:

          <div id="mask" style="display:none;"></div>

           

          <div id="id_popdiv" style="display:none;" class="popup">

              <iframe id="id_popiframe" src="" frameborder="0" scrolling="no" width="100%" height="100%">

              </iframe>

          </div>

           

          iframe有點擊按鈕的html代碼

          <script type="text/javascript" src="js/popmanager.js"></script>

           

          <a href="javascript:pop('popcontenturl',782,600);" class="link" rel="1">show pop content</a><br />

           

          popcontenturl 頁面中的關閉的主要代碼:

          <script type="text/javascript" src="js/showhide.js" charset="utf-8"></script>

          <div class="pop_container">

                   <a href="javascript:unpop();" title="關閉" class="close"></a>

                   <h2 class="title">浮層標題</h2>

              <div class="pop_content">

                 浮層內容

              </div>

          </div>

           

           

          主要js代碼(popmanager.js)

          function getWindowScrollTop(win){

                   var scrollTop=0;

                   if(win.document.documentElement&&win.document.documentElement.scrollTop){

                             scrollTop=win.document.documentElement.scrollTop;

                   }else if(win.document.body){

                             scrollTop=win.document.body.scrollTop;

                   }

                   return scrollTop;

          }

          function setWindowScrollTop(win, topHeight)

          {

              if(win.document.documentElement)

              {

                  win.document.documentElement.scrollTop = topHeight;

              }

              if(win.document.body){

                  win.document.body.scrollTop = topHeight;

              }

          }

          function getWindowScrollLeft(win){

                   var scrollLeft=0;

                   if(win.document.documentElement&&win.document.documentElement.scrollLeft){

                             scrollLeft=win.document.documentElement.scrollLeft;

                   } else if(win.document.body){

                             scrollLeft=win.document.body.scrollLeft;

                   }

                   return scrollLeft;

          }

          function getWindowHeight(win){

                   var clientHeight=0;

                   if(win.document.body.clientHeight&&win.document.documentElement.clientHeight){

                             clientHeight = (win.document.body.clientHeight<win.document.documentElement.clientHeight)?

                      win.document.body.clientHeight:win.document.documentElement.clientHeight;

                   }else{

                             clientHeight = (win.document.body.clientHeight>win.document.documentElement.clientHeight)?

                      win.document.body.clientHeight:win.document.documentElement.clientHeight;

                   }

                   return clientHeight;

          }

          function getWindowWidth(win){

                   var clientWidth=0;

                   if(win.document.body.clientWidth&&win.document.documentElement.clientWidth){

                             clientWidth = (win.document.body.clientWidth<win.document.documentElement.clientWidth)?

                      win.document.body.clientWidth:win.document.documentElement.clientWidth;

                   }else{

                             clientWidth = (win.document.body.clientWidth>win.document.documentElement.clientWidth)?

                      win.document.body.clientWidth:win.document.documentElement.clientWidth;

                   }

                   return clientWidth;

          }

           

          function unpop()

          {

              try{

                  var win = (top && top!=self)?top:window;

              }

              catch(e)

              {

                  return ;

              }

             

              win.document.getElementById('mask').style.display = "none";

              win.document.getElementById("id_popdiv").style.display = "none";

              win.document.getElementById("id_iframe_pop").setAttribute('src', '');

             

          }

           

          function pop(url,width,height)

          {

              try{

                  var win = (top && top!=self)?top:window;

              }

              catch(e)

              {

                  return ;

              }

             

             

              var topWindowHeight = getWindowHeight(win);

              var topWindowWidth = getWindowWidth(win);

           

              var lvTop=parseInt((topWindowHeight-height)/2)+parseInt(getWindowScrollTop(win));

              var lvLeft=parseInt((topWindowWidth-width)/2)+parseInt(getWindowScrollLeft(win));

              lvTop = lvTop<=0?1:lvTop;

              lvLeft = lvLeft<=0?1:lvLeft;

             

           

              win.document.getElementById("id_popdiv").style.top=lvTop+"px";

              win.document.getElementById("id_popdiv").style.left=lvLeft+"px";

              win.document.getElementById("id_popdiv").style.margin="0";

             

              win.document.getElementById("id_iframe_pop").setAttribute('src', url);

           

                      

              win.document.getElementById("id_iframe_pop").setAttribute('width', width);

              win.document.getElementById("id_iframe_pop").setAttribute('height', height);

             

              win.document.getElementById('mask').style.display = "block";

              win.document.getElementById("id_popdiv").style.display = "block";

          }

           

           

           

           

          附:

          應用較多的div浮層技術為基于jquery的blockUI技術。詳情請參考相關文檔。

          轉自:
          http://www.cnblogs.com/zhanghairong/archive/2012/07/11/2586050.html

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 准格尔旗| 汉寿县| 辰溪县| 乐至县| 桑植县| 桓台县| 宁河县| 濮阳市| 绥德县| 九龙城区| 科技| 新密市| 恩平市| 思南县| 兴山县| 长武县| 天峨县| 安仁县| 琼海市| 开鲁县| 西乌| 普格县| 平南县| 日照市| 南江县| 阳高县| 丹凤县| 通河县| 息烽县| 藁城市| 平湖市| 巧家县| 区。| 胶南市| 固始县| 玛沁县| 女性| 榆社县| 明光市| 白山市| 法库县|