DIV CSS 佈局教程網

為瀏覽器視窗加上遮罩層:從原理到相容性處理
編輯:JavaScript基礎知識     

背景

在 Web 2.0 的互動模式中,頁面彈窗是非常常見的做法,既能避免不必要的頁面跳轉,也能改善版面配置與互動性。但瀏覽器原生的彈窗函數(alert、confirm、prompt)限制很大:外觀難以客製,行為也不夠靈活,因此我們經常需要自行實作彈窗。

而當我們要做的是「模態彈窗」(出現時頁面其他地方不可點擊),標準做法就是用一個 div 把整個視窗遮擋起來。

Step 1:基本遮罩層

<div unselectable="on" style="background:#000;filter:alpha(opacity=10);opacity:.1;left:0px;top:0px;position:fixed;height:100%;width:100%;overflow:hidden;z-index:10000;"></div>

這段 HTML + CSS 就能實現遮擋瀏覽器視窗,其中幾個關鍵屬性分別是:

1) 顏色與透明度:background:#000 把背景設為黑色;filter:alpha(opacity=10) 在 IE 下設定透明度為 0.1;opacity:.1 則用於非 IE 瀏覽器。三者結合,就得到了「被擋住但仍可見」的效果。

2) 尺寸與定位:left:0;top:0;position:fixed;height:100%;width:100% 讓 div 覆蓋整個視窗。這裡有個小技巧——當 div 的 position 是 fixed 或 absolute 時,把高度設為百分比(如 100%),瀏覽器會以可視區域(viewport)的高度作為計算基準。而設為 fixed 還有一個好處:即使頁面捲動或視窗縮放,遮罩層也會一直蓋在可視區域上。

3) overflow:hidden:避免遮罩層自身撐出捲軸。

Step 2:處理 IE6

上面的程式碼在 IE6 下會失效,原因有兩個:一來 IE6 不支援 position:fixed;二來更麻煩的是,IE6 中 height:100% 不起作用,div 高度不再參照可視區域。

第一個問題用 CSS hack 就能解決,加上 _position:absolute 即可。第二個問題則需要 JS 動態計算遮罩層的高與寬,特別要注意:為了保證頁面捲動時遮罩層仍然蓋住視窗,計算出的高寬必須涵蓋整個捲動區域。

function calculateSize() {
  var b = document.documentElement.clientHeight ? document.documentElement : document.body,
  height = b.scrollHeight > b.clientHeight ? b.scrollHeight : b.clientHeight,
  width = b.scrollWidth > b.clientWidth ? b.scrollWidth : b.clientWidth;
  mask.css({height: height, width: width});
}

此外,視窗尺寸變化時必須重新計算,否則新擴大的區域會露出來:

function resize() {
  calculateSize();
  $(window).on("resize", calculateSize);
}

Step 3:IE6 的 select 元素 bug

在 IE6 下還有一個著名問題:頁面上若存在 select 元素,遮罩層無法遮住它。解法是在遮罩層內額外加一層 iframe。

<div unselectable="on" style="display:none;background:#000;filter:alpha(opacity=10);opacity:.1;left:0px;top:0px;position:fixed;_position:absolute;height:100%;width:100%;overflow:hidden;z-index:10000;">
  <div style="position:absolute;width:100%;height:100%;top:0;left:0;z-index:10;background-color:#000"></div>
  <iframe border="0" frameborder="0" style="width:100%;height:100%;position:absolute;top:0;left:0;z-index:1"></iframe>
</div>

有兩個小技巧需要說明:

1) iframe 使用 width:100%;height:100% 是可行的,因為它的定位父元素尺寸已經確定。

2) 遮罩層內部除了 iframe,還多包了一個 div,兩者的 position 都是 absolute,而 div 的 z-index 大於 iframe。這樣一來,內層 div 就蓋住了 iframe。這具有現實意義:頁面上的一些事件(如 onclick、onmouseup、onmousemove)仍然會被本頁面的元素接收,而不是被 iframe 攔截。

完整程式碼

var windowMask = (function($) {

var isIE6 = $.browser.msie && $.browser.version == "6.0";
var mask = '<div unselectable="on" style="display:none;background:#000;filter:alpha(opacity=10);opacity:.1;left:0px;top:0px;position:fixed;height:100%;width:100%;overflow:hidden;z-index:10000;"></div>';

isIE6 && (mask = '<div unselectable="on" style="display:none;background:#000;filter:alpha(opacity=10);opacity:.1;left:0px;top:0px;position:fixed;_position:absolute;height:100%;width:100%;overflow:hidden;z-index:10000;"><div style="position:absolute;width:100%;height:100%;top:0;left:0;z-index:10;background-color:#000"></div><iframe border="0" frameborder="0" style="width:100%;height:100%;position:absolute;top:0;left:0;z-index:1"></iframe></div>');

mask = $(mask);
$("body").append(mask);

function show() {
  isIE6 && resize();
  mask.show();
}

function hide() {
  isIE6 && $(window).off("resize", calculateSize);
  mask.hide();
}

function calculateSize() {
  var b = document.documentElement.clientHeight ? document.documentElement : document.body,
  height = b.scrollHeight > b.clientHeight ? b.scrollHeight : b.clientHeight,
  width = b.scrollWidth > b.clientWidth ? b.scrollWidth : b.clientWidth;

  mask.css({height: height, width: width});
}

function resize() {
  calculateSize();
  $(window).on("resize", calculateSize);
}

return {
  show: show,
  hide: hide
};
})();

使用方式很簡單:需要顯示遮罩層時呼叫 windowMask.show(),要移除時呼叫 windowMask.hide()。

最後補一句現況:$.browser 已在 jQuery 1.9 中移除,IE6 也早已退出主流,因此新專案可以直接用 position:fixed 搭配 z-index 完成,把這份實作當作理解原理的參考即可。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved