DIV CSS 佈局教程網

用 JS + CSS 做彈出視窗並把背景壓暗
編輯:關於JavaScript     

彈跳視窗要突出來,最簡單的辦法就是把背後的一切壓暗。做法是鋪一層半透明的遮罩蓋滿整個視窗,再把彈窗放在遮罩之上。這個效果只需要兩個 div 與兩個函式。

完整程式碼

<html>
<head>
<title>JS+CSS 實作的 Div 彈出視窗,同時背景變暗</title>
<script>
  // 開啟:顯示遮罩並鋪滿整個可見區域,再顯示彈窗
  function locking() {
    document.all.ly.style.display = "block";
    document.all.ly.style.width  = document.body.clientWidth;
    document.all.ly.style.height = document.body.clientHeight;
    document.all.Layer2.style.display = 'block';
  }

  // 關閉:把兩層都藏起來
  function Lock_CheckForm(theForm) {
    document.all.ly.style.display = 'none';
    document.all.Layer2.style.display = 'none';
    return false;
  }
</script>
<style type="text/css">
.STYLE1 { font-size: 12px; }
a:link    { color: #000; text-decoration: none; }
a:visited { text-decoration: none; }
a:hover   { text-decoration: none; }
a:active  { text-decoration: none; }
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
  <p align="center">
    <input type="button" value="彈出 DIV" onClick="locking()" />
  </p>

  <!-- 遮罩層:半透明、鋪滿全螢幕 -->
  <div id="ly" style="position: absolute; top: 0; filter: alpha(opacity=60);
       background-color: #777; z-index: 2; left: 0; display: none;"></div>

  <!-- 彈窗層 -->
  <div id="Layer2" align="center" style="position: absolute; z-index: 3;
       left: expression((document.body.offsetWidth-540)/2);
       top: expression((document.body.offsetHeight-170)/10);
       background-color: #fff; display: none;">
    <table width="540" height="300" border="0" cellpadding="0" cellspacing="0"
           style="border: 1px solid #e7e3e7; border-collapse: collapse;">
      <tr>
        <td style="background-color: #73A2d6; color: #fff; padding-left: 4px;
             padding-top: 2px; font-weight: bold; font-size: 12px;"
             height="10" valign="middle">
          <div align="right">
            <a href="JavaScript:;" class="STYLE1" onclick="Lock_CheckForm(this);">[關閉]</a>
          </div>
        </td>
      </tr>
      <tr>
        <td height="130" align="center">
          <br><br><hr>
          <p align="center"><font color="red">這裡放彈窗的內容</font></p>
        </td>
      </tr>
    </table>
  </div>
</body>
</html>

它是怎麼運作的

遮罩層 ly 用 position: absolute 定位在左上角,寬高在 JS 中被設成 body 的 clientWidth/clientHeight,於是正好蓋滿可見區域。filter: alpha(opacity=60) 讓它半透明,背後的內容就「變暗」了。z-index 是靈魂:遮罩 2、彈窗 3,數字大的在上層,所以彈窗浮在遮罩之上、遮罩又蓋住頁面。

彈窗的水平置中靠 left: expression((document.body.offsetWidth-540)/2),也就是「(視窗寬 - 彈窗寬) / 2」。expression 是 IE 專屬語法,會持續重新計算,效能很差。

這段程式今天的問題

第一,document.all 是 IE 專屬,現在應改用 document.getElementById。

第二,CSS expression 只在 IE 有效,而且會不斷求值造成效能問題,必須淘汰。置中改用 transform: translate(-50%, -50%) 搭配 top: 50%; left: 50%,或直接用 flex 置中,一行 CSS 就解決。

第三,遮罩用 absolute 定位時,若頁面有捲動,遮罩不會跟著移動,捲到下面就露出沒被壓暗的區域。應改用 position: fixed,它相對視窗定位,永遠蓋滿可見範圍。

第四,opacity 是標準屬性,filter: alpha() 可以一併留作退路或直接移除。

現代寫法

<style>
#ly {
  display: none;
  position: fixed;              /* 相對視窗,捲動也不會露底 */
  top: 0; left: 0;
  width: 100%; height: 100%;
  background: rgba(0, 0, 0, .6); /* 半透明黑,最乾淨的壓暗做法 */
  z-index: 2;
}
#Layer2 {
  display: none;
  position: fixed;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  background: #fff;
  z-index: 3;
}
</style>

用 rgba 當背景色比 opacity 更好,因為 opacity 會連子元素一起變透明,rgba 只影響這一層。

另外還有三件事值得補上:一是點擊遮罩關閉彈窗(判定 e.target 是否為遮罩本身);二是 Esc 鍵關閉;三是開啟時鎖住 body 的捲動(document.body.style.overflow = 'hidden'),關閉時還原,否則使用者可以在彈窗開著時捲動背後的頁面。

最後提醒可見性:彈窗開啟時,焦點應該移進彈窗並鎖定在內部(focus trap),同時為遮罩加上 aria-hidden="true"、為彈窗加上 role="dialog" 與 aria-modal="true",鍵盤與螢幕閱讀器使用者才能正確操作。若不想自己處理這些細節,<dialog> 元素與 showModal() 已經內建了遮罩、Esc 關閉與焦點管理。

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