這個示例的看點有兩個:一是用純 CSS 拼出圓角邊框(那個年代還沒有 border-radius),二是彈出層可以拖動,並帶有半透明遮罩。兩個技巧組合起來,就是一個相當完整的對話框實現。
核心思路是用三層 <u> 標籤疊出漸變的圓角。每層的高度都是 1px,通過左右 margin 的遞減,讓背景色一層層向外擴展,視覺上就成了弧形:
U {
display: block;
overflow: hidden;
height: 1px;
}
U.f1 { background-color:#5cc; margin: 0px 3px; } /* 最頂層,左右各留 3px */
U.f2 { background-color:#5cc; margin: 0px 1px;
border-right: #5cc 2px solid; border-left: #5cc 2px solid; }
U.f3 { background-color:#5cc; margin: 0px 1px;
border-right: #5cc 1px solid; border-left: #5cc 1px solid; }
使用時在頂部與底部各放三層,順序相反即可:
<U class="f1"></U><U class="f2"></U><U class="f3"></U>
<div class="d_top">...</div>
<div class="d_body">...</div>
<div class="d_foot"></div>
<U class="f3"></U><U class="f2"></U><U class="f1"></U>
這種做法在 CSS3 之前非常流行,代號通常叫「魔力方塊」或「滑門」。今天一句 border-radius: 8px; 就能完全替代,但理解它的原理有助於明白「用有限屬性拼出曲線」這類思路。
.al {
opacity: 0.80;
filter: progid:DXImageTransform.Microsoft.Alpha(
style=0, opacity=80, finishOpacity=100);
}
.al2 {
opacity: 0.00;
filter: progid:DXImageTransform.Microsoft.Alpha(
style=0, opacity=0, finishOpacity=100);
}
值得注意的是示例在遮罩裡額外放了一個透明的 iframe(用 al2 設為完全透明)。這是為了繞開舊版 IE 一個著名 bug:IE6 中的 select 元素會穿透任何 z-index 更高的層,而插入一個 iframe 墊在遮罩下方就能遮住它。這是那個時代定位彈出層的必備防護。
<script type="text/javascript">
function $(id){ return document.getElementById(id); }
function show(id){
var t = $(id);
t.style.width = document.body.clientWidth;
t.style.height = document.body.clientHeight;
window.onresize = function(){
t.style.width = document.body.clientWidth;
t.style.height = document.body.clientHeight;
};
$(id).style.display = "";
}
function cl(id){
$(id).style.display = "none";
}
function moveEvent(e, id){
var isIE = (document.all) ? true : false;
drag = true;
xx = isIE ? event.x : e.pageX;
yy = isIE ? event.y : e.pageY;
L = document.getElementById(id).offsetLeft;
T = document.getElementById(id).offsetTop;
document.onmousemove = function(e) {
if (drag) {
x = isIE ? event.x : e.pageX; if (x < 0) x = 0;
y = isIE ? event.y : e.pageY; if (y < 0) y = 0;
document.getElementById(id).style.left = L - xx + x;
document.getElementById(id).style.top = T - yy + y;
}
};
document.onmouseup = function(){
drag = false;
};
}
window.onscroll = function(){
$("back_div").style.width = document.body.scrollWidth;
$("back_div").style.height = document.body.scrollHeight;
};
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JS+CSS實現的可拖拽的漂亮圓角效果的彈出層</title>
<style>
body{ margin:0px; padding:0px; font-size:14px; }
#a { float:left; }
#a a { text-decoration:none; }
.al {
opacity: 0.80;
filter: progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=80,finishOpacity=100);
}
.al2 {
opacity: 0.00;
filter: progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0,finishOpacity=100);
}
U { display:block; overflow:hidden; height:1px; }
U.f1 { background-color:#5cc; margin:0px 3px; }
U.f2 { background-color:#5cc; margin:0px 1px;
border-right:#5cc 2px solid; border-left:#5cc 2px solid; }
U.f3 { background-color:#5cc; margin:0px 1px;
border-right:#5cc 1px solid; border-left:#5cc 1px solid; }
.d_top {
clear:both; overflow:hidden;
background-color:#5cc; height:29px; vertical-align:bottom;
}
.d_top a {
float:right; margin-top:5px; margin-right:13px;
padding-top:3px; width:18px; color:red;
background-color:#789; text-decoration:none;
font-weight:bold; text-align:center; vertical-align:middle;
}
.d_top span {
float:left; font-size:13px; margin-left:15px; margin-top:8px;
}
.d_body {
border-right:#5cc 3px solid; border-left:#5cc 3px solid;
padding:10px; height:200px; background-color:#fff;
}
.d_foot { clear:both; overflow:hidden; background-color:#5cc; height:2px; }
</style>
<script type="text/javascript">
function $(id){ return document.getElementById(id); }
function show(id){
var t = $(id);
t.style.width = document.body.clientWidth;
t.style.height = document.body.clientHeight;
window.onresize = function(){
t.style.width = document.body.clientWidth;
t.style.height = document.body.clientHeight;
};
$(id).style.display = "";
}
function cl(id){ $(id).style.display = "none"; }
function moveEvent(e, id){
var isIE = (document.all) ? true : false;
drag = true;
xx = isIE ? event.x : e.pageX;
yy = isIE ? event.y : e.pageY;
L = document.getElementById(id).offsetLeft;
T = document.getElementById(id).offsetTop;
document.onmousemove = function(e) {
if (drag) {
x = isIE ? event.x : e.pageX; if (x < 0) x = 0;
y = isIE ? event.y : e.pageY; if (y < 0) y = 0;
document.getElementById(id).style.left = L - xx + x;
document.getElementById(id).style.top = T - yy + y;
}
};
document.onmouseup = function(){ drag = false; };
}
window.onscroll = function(){
$("back_div").style.width = document.body.scrollWidth;
$("back_div").style.height = document.body.scrollHeight;
};
</script>
</head>
<body>
<div id="a" style="position:absolute; left:300px; top:200px;">
<a href="javascript:" onClick="show('t')">點這裡彈出圓角效果的 div</a>
</div>
<div id="t" style="display:none;">
<!-- 遮罩層 -->
<div style="width:100%; height:100%; z-index:-1; position:absolute;
float:left; background-color:#555; overflow:hidden;"
class="al" id="back_div">
<!-- 墊在遮罩下方的透明 iframe,用於兼容舊 IE 的 select 穿透 -->
<iframe style="position:absolute; left:0px; top:0px; float:left;
z-index:-1; margin:0px; padding:0px;"
frameborder="0" scrolling="no" width="100%" height="2000px"
class="al2" id="ifr"></iframe>
</div>
<!-- 對話框本體 -->
<DIV style="width:500px; position:absolute; float:left;
top:25%; left:30%; z-index:999;
clear:both; overflow:hidden;" id="t_div">
<U class="f1"></U><U class="f2"></U><U class="f3"></U>
<div class="d_top" onMouseDown="moveEvent(event,'t_div')">
<span>歡迎光臨</span>
<a href="javascript:" onClick="cl('t')">×</a>
</div>
<DIV class="d_body">歡迎光臨:</DIV>
<div class="d_foot"></div>
<U class="f3"></U><U class="f2"></U><U class="f1"></U>
</DIV>
</div>
</body>
</html>
· 拖拽坐標換算有誤。示例用 style.left = L - xx + x,其中 xx 是鼠標按下時的絕對坐標,x 是當前絕對坐標,兩者相減才是位移量,邏輯上成立。但它同時用 if (x < 0) x = 0 做夾值,會破壞偏移量的連續性,導致拖到邊界後往回拖時出現跳動。正確做法是夾「計算後的 left/top」而非夾鼠標坐標。
· 缺少上邊界約束。只判斷了 x < 0 與 y < 0,沒有防止拖出右側與底部。
· 拖拽變量未用 var 聲明。drag、xx、yy、L、T、x、y 全部洩漏為全局變量,容易與其他腳本衝突。
· id 重複。示例中 id="ifr" 出現了兩次,違反 HTML 規範。
· 遮罩未阻止滾動穿透。彈出層打開時,滾輪仍能滾動底層頁面。應在打開時給 body 加 overflow:hidden,關閉時還原。
· 遮罩點擊無法關閉。點擊遮罩區域不會關閉彈窗,這與當代的交互習慣不符。
· window.onresize 與 window.onscroll 直接賦值會覆蓋其他處理器,應改用 addEventListener。
· 沒有可訪問性支持。缺少 role="dialog"、aria-modal="true"、焦點陷阱與 Esc 關閉。
<style>
.overlay{
position: fixed; inset: 0;
background: rgba(0,0,0,.5);
display: flex; align-items: center; justify-content: center;
z-index: 1000;
}
.dialog{
width: 500px; max-width: 90vw;
background: #fff;
border: 3px solid #5cc;
border-radius: 8px; /* 一句話取代三層 <u> */
box-shadow: 0 10px 40px rgba(0,0,0,.3);
overflow: hidden;
}
.dialog header{
background: #5cc; color: #fff;
padding: 8px 15px;
display: flex; justify-content: space-between; align-items: center;
cursor: move;
user-select: none; /* 拖拽時防止選中文本 */
}
.dialog header button{
border: 0; background: transparent; color: #fff;
font-size: 18px; cursor: pointer; line-height: 1;
}
.dialog .body{ padding: 15px; }
</style>
<div class="overlay" id="overlay" role="dialog" aria-modal="true" aria-labelledby="dlgTitle">
<div class="dialog" id="dialog">
<header id="dragHandle">
<span id="dlgTitle">歡迎光臨</span>
<button id="closeBtn" aria-label="關閉對話框">×</button>
</header>
<div class="body">歡迎光臨!</div>
</div>
</div>
<script>
const overlay = document.getElementById('overlay');
const dialog = document.getElementById('dialog');
const handle = document.getElementById('dragHandle');
// 打開 / 關閉
function openDialog(){
overlay.style.display = 'flex';
document.body.style.overflow = 'hidden'; // 阻止背景滾動
document.getElementById('closeBtn').focus();
}
function closeDialog(){
overlay.style.display = 'none';
document.body.style.overflow = '';
}
document.getElementById('closeBtn')
.addEventListener('click', (e) => { e.stopPropagation(); closeDialog(); });
overlay.addEventListener('click', closeDialog); // 點遮罩關閉
dialog.addEventListener('click', (e) => e.stopPropagation());
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') closeDialog();
});
// 拖拽:Pointer Events 同時支援鼠標與觸屏
let dragging = false, startX = 0, startY = 0, originL = 0, originT = 0;
handle.addEventListener('pointerdown', (e) => {
dragging = true;
startX = e.clientX; startY = e.clientY;
const rect = dialog.getBoundingClientRect();
originL = rect.left; originT = rect.top;
handle.setPointerCapture(e.pointerId);
e.preventDefault();
});
handle.addEventListener('pointermove', (e) => {
if (!dragging) return;
const maxL = window.innerWidth - dialog.offsetWidth;
const maxT = window.innerHeight - dialog.offsetHeight;
const l = Math.max(0, Math.min(originL + e.clientX - startX, maxL));
const t = Math.max(0, Math.min(originT + e.clientY - startY, maxT));
dialog.style.position = 'fixed';
dialog.style.left = l + 'px';
dialog.style.top = t + 'px';
});
handle.addEventListener('pointerup', () => { dragging = false; });
handle.addEventListener('pointercancel', () => { dragging = false; });
openDialog();
</script>
對比之下,現代版本沒有了那三層 <u>、沒有了透明 iframe、沒有了 document.body.scrollWidth 的手工計算——position:fixed 加 inset:0 讓遮罩永遠貼合視窗,border-radius 一行搞定圓角,flex 自動完成居中,Pointer Events 統一了輸入設備。原示例的價值在於完整展示了彈出層需要處理的那些環節:遮罩、居中、拖拽、邊界、關閉、滾動鎖定——這些問題今天依然存在,只是有了更優雅的答案。