<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
/* 遮罩層 */
#overlay {
position: fixed; inset: 0; background: rgba(0,0,0,.5);
display: none; z-index: 998;
}
/* 彈出層 */
#dialog {
position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%);
width: 500px; background: #fff; border-radius: 8px;
box-shadow: 0 4px 24px rgba(0,0,0,.2);
display: none; z-index: 999; overflow: hidden;
}
#dialog .header {
height: 36px; line-height: 36px; padding: 0 16px;
background: #5cc; color: #fff; font-weight: bold;
cursor: move; display: flex; justify-content: space-between;
}
#dialog .header .close { background: #789; color: red; width: 24px; text-align: center; text-decoration: none; border-radius: 2px; }
#dialog .body { padding: 20px; min-height: 150px; }
#dialog .footer { height: 4px; background: #5cc; } /* 底部圓角收尾 */
</style>
</head>
<body>
<button id="openBtn">開啟彈出層</button>
<div id="overlay"></div>
<div id="dialog">
<div class="header">
<span>歡迎光臨</span>
<a class="close" href="javascript:;">×</a>
</div>
<div class="body">這是可拖曳的圓角彈出層內容區。</div>
<div class="footer"></div>
</div>
<script>
const overlay = document.getElementById('overlay');
const dialog = document.getElementById('dialog');
const header = dialog.querySelector('.header');
const closeBtn = dialog.querySelector('.close');
const openBtn = document.getElementById('openBtn');
let drag = false, offsetX, offsetY;
function open() {
overlay.style.display = 'block';
dialog.style.display = 'block';
// 置中(若尺寸動態變化可在這裡重算)
}
function close() {
overlay.style.display = 'none';
dialog.style.display = 'none';
}
openBtn.onclick = open;
closeBtn.onclick = close;
overlay.onclick = close; // 點遮罩關閉
// 拖曳邏輯
header.onmousedown = e => {
drag = true;
offsetX = e.clientX - dialog.offsetLeft;
offsetY = e.clientY - dialog.offsetTop;
document.body.style.userSelect = 'none';
};
document.onmousemove = e => {
if (!drag) return;
dialog.style.left = e.clientX - offsetX + 'px';
dialog.style.top = e.clientY - offsetY + 'px';
dialog.style.transform = 'none'; // 拖曳後取消置中 transform
};
document.onmouseup = () => { drag = false; document.body.style.userSelect = ''; };
// 視窗縮放時重新置中(若未被拖曳)
window.onresize = () => {
if (!drag) { dialog.style.left = '50%'; dialog.style.top = '50%'; dialog.style.transform = 'translate(-50%, -50%)'; }
};
</script>
</body>
</html>
<u class="f1/f2/f3"> 疊加 1px 高度區塊模擬圓角(CSS3 border-radius 普及前的 Hack)。filter:Alpha(opacity=80) + 標準 opacity。<iframe> 遮罩 select 元素(IE6 z-index Bug)。onmousedown 記錄偏移,onmousemove 更新 left/top。現代專案請直接使用 CSS3 border-radius、box-shadow、position:fixed 實現,程式碼量減少 80% 且效能更好。原始碼展示了早期前端為相容 IE6/7 所做的大量 Hack,具有歷史參考價值。