點一下按鈕,畫面中央彈出一個視窗,背景同時變暗——這是「燈箱」效果的基本型。這份範例不依賴任何套件,用兩個絕對定位的 div 加上一層半透明遮罩就完成,而且視窗標題列還可以用滑鼠拖動。
函式 alertWin(標題, 內容, 寬, 高) 會動態產生兩個元素:bgObj 是鋪滿整個視窗的半透明黑色遮罩,msgObj 是真正要顯示的對話框。兩者用 z-index 分層,遮罩 101、視窗 102。
function alertWin(title, msg, w, h) {
var titleheight = "23px";
var bordercolor = "#336699";
var titlecolor = "#FFFFFF";
var titlebgcolor = "#336699";
var bgcolor = "#FFFFFF";
var iWidth = document.documentElement.clientWidth;
var iHeight = document.documentElement.clientHeight;
// 1. 建立遮罩,鋪滿整個視窗並半透明
var bgObj = document.createElement("div");
bgObj.style.cssText =
"position:absolute;left:0px;top:0px;width:" + iWidth + "px;" +
"height:" + Math.max(document.body.clientHeight, iHeight) + "px;" +
"filter:Alpha(Opacity=30);opacity:0.3;background-color:#000000;z-index:101;";
document.body.appendChild(bgObj);
// 2. 建立視窗本體,水平垂直置中
var msgObj = document.createElement("div");
msgObj.style.cssText =
"position:absolute;font:11px '宋體';" +
"top:" + (iHeight - h) / 2 + "px;left:" + (iWidth - w) / 2 + "px;" +
"width:" + w + "px;height:" + h + "px;text-align:center;" +
"border:1px solid " + bordercolor + ";background-color:" + bgcolor + ";" +
"padding:1px;line-height:22px;z-index:102;";
document.body.appendChild(msgObj);
// 3. 用 table 排出標題列與內容區
var table = document.createElement("table");
msgObj.appendChild(table);
table.style.cssText = "margin:0px;border:0px;padding:0px;";
table.cellSpacing = 0;
var tr = table.insertRow(-1);
var titleBar = tr.insertCell(-1);
titleBar.style.cssText =
"width:100%;height:" + titleheight + "px;text-align:left;padding:3px;" +
"font:bold 13px '宋體';color:" + titlecolor + ";" +
"border:1px solid " + bordercolor + ";cursor:move;background-color:" + titlebgcolor;
titleBar.style.paddingLeft = "10px";
titleBar.innerHTML = title;
// 4. 讓標題列可以拖曳
var moveX = 0, moveY = 0, moveTop = 0, moveLeft = 0, moveable = false;
var docMouseMoveEvent = document.onmousemove;
var docMouseUpEvent = document.onmouseup;
titleBar.onmousedown = function () {
var evt = getEvent();
moveable = true;
moveX = evt.clientX; moveY = evt.clientY;
moveTop = parseInt(msgObj.style.top);
moveLeft = parseInt(msgObj.style.left);
document.onmousemove = function () {
if (moveable) {
var evt = getEvent();
var x = moveLeft + evt.clientX - moveX;
var y = moveTop + evt.clientY - moveY;
// 限制在視窗範圍內,避免拖出畫面
if (x > 0 && (x + w < iWidth) && y > 0 && (y + h < iHeight)) {
msgObj.style.left = x + "px";
msgObj.style.top = y + "px";
}
}
};
document.onmouseup = function () {
if (moveable) {
document.onmousemove = docMouseMoveEvent;
document.onmouseup = docMouseUpEvent;
moveable = false;
moveX = 0; moveY = 0; moveTop = 0; moveLeft = 0;
}
};
};
// 5. 關閉按鈕:把兩個元素都移除
var closeBtn = tr.insertCell(-1);
closeBtn.style.cssText = "cursor:pointer;padding:2px;background-color:" + titlebgcolor;
closeBtn.innerHTML = "<span style='font-size:15pt;color:" + titlecolor + ";'>×</span>";
closeBtn.onclick = function () {
document.body.removeChild(bgObj);
document.body.removeChild(msgObj);
};
// 6. 內容區
var msgBox = table.insertRow(-1).insertCell(-1);
msgBox.style.cssText = "font:10pt '宋體';";
msgBox.colSpan = 2;
msgBox.innerHTML = msg;
// 取得 Event 物件,同時相容 IE 與 Firefox
function getEvent() {
return window.event || arguments.callee.caller.arguments[0];
}
}
<input type="button" value="點這裡"
onclick="alertWin('點擊彈出可關閉的層窗口,網頁變灰', 290, 208);" />
遮罩高度取 Math.max(document.body.clientHeight, iHeight),是為了在內容比視窗長時也能蓋滿整個可捲動區域。拖曳時額外判斷邊界,避免視窗被拖到畫面外就再也拉不回來。滑鼠放開時把 onmousemove 還原成原本的處理器,避免影響頁面上其他拖曳功能。
這段程式寫於 IE 為主流的時代,因此同時照顧了 filter:Alpha 與標準 opacity。今日重寫時,建議用 position:fixed 讓遮罩自動符合視窗大小、以 addEventListener 取代 arguments.callee(後者在 strict mode 中已禁用),並在開啟時鎖住背景捲動、關閉後把焦點還給原本的按鈕,同時加上 role="dialog" 與 aria-modal="true",讓鍵盤與螢幕閱讀器使用者也能正確操作。