萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

JS+CSS 實現遮罩居中彈窗:跟隨滾動、IE6 兼容、無依賴

核心 CSS 佈局策略

/* 遮罩層:固定鋪滿視口 */
#maskLayer {
    position: fixed;        /* 現代瀏覽器 */
    _position: absolute;    /* IE6 hack */
    top: 0; left: 0;
    width: 100%; height: 100%;
    background: #000;
    opacity: 0.5;
    filter: alpha(opacity=50); /* IE8- */
    z-index: 999;
    display: none;
    /* IE6 滾動跟隨 hack */
    _left: expression(documentElement.scrollLeft + documentElement.clientWidth - this.offsetWidth);
    _top: expression(documentElement.scrollTop + documentElement.clientHeight - this.offsetHeight);
}

/* 彈窗層:絕對居中 */
#popup {
    position: fixed;
    _position: absolute;    /* IE6 */
    left: 50%; top: 50%;
    width: 300px;
    margin-left: -150px;    /* 寬度一半負邊距 */
    z-index: 1000;
    display: none;
    /* IE6 居中 hack */
    _top: expression(eval(document.compatMode && document.compatMode=='CSS1Compat') ?
        documentElement.scrollTop + (document.documentElement.clientHeight - this.offsetHeight)/2 :
        document.body.scrollTop + (document.body.clientHeight - this.clientHeight)/2);
    _margin-left: 0;        /* IE6 用 top/left 定位,不需 margin */
}

完整 HTML 結構

<!DOCTYPE html>
<html><head><meta charset="utf-8">
<style>/* 上述 CSS */</style></head>
<body>
<div class="wrap">
    <p>頁面內容...</p>
    <button id="openBtn">打開彈窗</button>
</div>

<div id="maskLayer"></div>
<div id="popup">
    <div class="content">
        <h3>我是居中彈窗</h3>
        <p>內容區域</p>
        <button id="closeBtn">關閉</button>
    </div>
</div>

<script>
(function() {
    var mask = document.getElementById('maskLayer');
    var popup = document.getElementById('popup');
    var btn = document.getElementById('openBtn');
    var closeBtn = document.getElementById('closeBtn');
    
    function open() {
        mask.style.display = 'block';
        popup.style.display = 'block';
        // 微調垂直居中 (fixed 時 margin-top 負一半高度)
        popup.style.marginTop = -popup.clientHeight / 2 + 'px';
    }
    
    function close() {
        mask.style.display = 'none';
        popup.style.display = 'none';
    }
    
    btn.onclick = open;
    closeBtn.onclick = close;
    mask.onclick = close; // 點擊遮罩關閉
    
    // ESC 關閉
    document.onkeydown = function(e) {
        if ((e || window.event).keyCode === 27) close();
    };
})();
</script>
</body></html>

關鍵技術點解析

1. 固定定位 + 負邊距居中

/* 標準寫法 */
position: fixed;
left: 50%; top: 50%;
width: 300px; height: 200px;
margin-left: -150px;
margin-top: -100px; /* 或 JS 動態計算 */

2. IE6 兼容方案

  • _position: absolute + expression() 動態計算 scrollTop
  • _left/_top expression 讓遮罩/彈窗跟隨滾動
  • html {_background: url(about:blank);} 防止 expression 閃爍

3. 現代瀏覽器優化

/* CSS transform 居中(無需已知寬高) */
#popup {
    position: fixed;
    left: 50%; top: 50%;
    transform: translate(-50%, -50%);
    /* 無需 margin 負值,適配動態內容 */
}

進階:模態框類封裝

var Modal = (function() {
    var $mask = $('

無障礙 (a11y) 增強

  • 打開時焦點移入彈窗,關閉時歸還原觸發元素
  • role="dialog" aria-modal="true" aria-labelledby="titleId"
  • Tab 循環限制在彈窗內 (focusin 事件攔截)
  • ESC 鍵關閉

此方案零依賴、體積極小、兼容至 IE6,適合不想引入重型 UI 庫的輕量級項目。

copyright © 萬盛學電腦網 all rights reserved