/* 遮罩層:固定鋪滿視口 */
#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 */
}
<!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>
/* 標準寫法 */
position: fixed;
left: 50%; top: 50%;
width: 300px; height: 200px;
margin-left: -150px;
margin-top: -100px; /* 或 JS 動態計算 */
_position: absolute + expression() 動態計算 scrollTop_left/_top expression 讓遮罩/彈窗跟隨滾動html {_background: url(about:blank);} 防止 expression 閃爍/* CSS transform 居中(無需已知寬高) */
#popup {
position: fixed;
left: 50%; top: 50%;
transform: translate(-50%, -50%);
/* 無需 margin 負值,適配動態內容 */
}
var Modal = (function() {
var $mask = $('').appendTo('body');
var $box = $('').appendTo('body');
return {
open: function(html, options) {
options = $.extend({width: 400, closeOnMask: true}, options);
$box.html(html).css({width: options.width, marginLeft: -options.width/2}).show();
$mask.show();
},
close: function() {
$box.hide().empty();
$mask.hide();
}
};
})();
// 使用
Modal.open('標題
內容
');
Modal.open('iframe:https://example.com', {width: 800}); // 需擴展解析
無障礙 (a11y) 增強
- 打開時焦點移入彈窗,關閉時歸還原觸發元素
role="dialog" aria-modal="true" aria-labelledby="titleId"
- Tab 循環限制在彈窗內 (
focusin 事件攔截)
- ESC 鍵關閉
此方案零依賴、體積極小、兼容至 IE6,適合不想引入重型 UI 庫的輕量級項目。
- 上一篇程序編程: JavaScript Math.ceil() 向上取整函數詳解與實用技巧
- 下一篇程序編程: JavaScript 對象聲明方式與數組操作全景指南