兩個經典效果:一個是半透明的遮罩提示框,一個是可以拖著走的浮動視窗。都不需要任何外掛。
<html>
<head>
<script>
function sAlert(txt) {
var shield = document.createElement("DIV");
shield.id = "shield";
shield.style.position = "absolute";
shield.style.left = "0px";
shield.style.top = "0px";
shield.style.width = "100%";
shield.style.height = document.body.scrollHeight + "px"; // 蓋滿整個可捲動區域
shield.style.background = "white";
shield.style.textAlign = "center";
shield.style.zIndex = "10000";
shield.style.filter = "alpha(opacity=80)"; // 舊 IE 專用
shield.style.opacity = 0.8; // 標準寫法
shield.innerHTML =
'<input type="button" value=" 確 定 " id="do_OK" onclick="doOk()" />';
document.body.appendChild(shield);
this.doOk = function () {
document.body.removeChild(shield);
};
document.getElementById("do_OK").focus();
}
</script>
</head>
<body>
<input type=button onclick="sAlert('登陸成功!')" value=登陸>
<p align=center><select><option>---</option></select></p>
</body>
</html>
幾個關鍵點:遮罩高度用 document.body.scrollHeight 而非視窗高度,這樣捲動時才不會露出破洞;opacity 與 filter 並存是為了相容舊版 IE;最後把焦點移到按鈕上,鍵盤操作才不會迷失。
<html>
<head>
<script>
var dragapproved = false;
var minrestore = 0; // 0 表示還原狀態,1 表示最大化狀態
var initialwidth, initialheight;
var ie5 = document.all && document.getElementById;
var ns6 = document.getElementById && !document.all;
function iecompattest() {
return (!window.opera && document.compatMode && document.compatMode != "BackCompat")
? document.documentElement : document.body;
}
// 拖曳中:依滑鼠位移更新視窗座標
function drag_drop(e) {
if (ie5 && dragapproved && event.button == 1) {
document.getElementById("dwindow").style.left = tempx + event.clientX - offsetx + "px";
document.getElementById("dwindow").style.top = tempy + event.clientY - offsety + "px";
} else if (ns6 && dragapproved) {
document.getElementById("dwindow").style.left = tempx + e.clientX - offsetx + "px";
document.getElementById("dwindow").style.top = tempy + e.clientY - offsety + "px";
}
}
// 按下時記錄起始點
function initializedrag(e) {
offsetx = ie5 ? event.clientX : e.clientX;
offsety = ie5 ? event.clientY : e.clientY;
document.getElementById("dwindowcontent").style.display = "none"; // 拖曳時隱藏內容
tempx = parseInt(document.getElementById("dwindow").style.left);
tempy = parseInt(document.getElementById("dwindow").style.top);
dragapproved = true;
document.getElementById("dwindow").onmousemove = drag_drop;
}
// 開啟視窗
function loadwindow(width, height) {
if (!ie5 && !ns6) {
return; // 非 IE / Netscape 可直接改用 window.open
}
document.getElementById("dwindow").style.display = '';
document.getElementById("dwindow").style.width = initialwidth = width + "px";
document.getElementById("dwindow").style.height = initialheight = height + "px";
document.getElementById("dwindow").style.left = "300px";
document.getElementById("dwindow").style.top =
ns6 ? window.pageYOffset * 1 + 30 + "px" : iecompattest().scrollTop * 1 + 30 + "px";
}
// 最大化 / 還原切換
function maximize() {
if (minrestore == 0) {
minrestore = 1;
document.getElementById("maxname").setAttribute("src", "layout.png");
document.getElementById("dwindow").style.width =
ns6 ? window.innerWidth - 20 + "px" : iecompattest().clientWidth + "px";
document.getElementById("dwindow").style.height =
ns6 ? window.innerHeight - 20 + "px" : iecompattest().clientHeight + "px";
} else {
minrestore = 0;
document.getElementById("maxname").setAttribute("src", "layout.png");
document.getElementById("dwindow").style.width = initialwidth;
document.getElementById("dwindow").style.height = initialheight;
}
document.getElementById("dwindow").style.left = ns6 ? window.pageXOffset + "px" : iecompattest().scrollLeft + "px";
document.getElementById("dwindow").style.top = ns6 ? window.pageYOffset + "px" : iecompattest().scrollTop + "px";
}
function closeit() {
document.getElementById("dwindow").style.display = "none";
}
// 放開滑鼠:結束拖曳並還原內容顯示
function stopdrag() {
dragapproved = false;
document.getElementById("dwindow").onmousemove = null;
document.getElementById("dwindowcontent").style.display = "";
}
</script>
</head>
<body>
<div id="dwindow"
style="position:absolute;background-color:#EBEBEB;cursor:hand;left:0px;top:0px;display:none"
onMousedown="initializedrag(event)"
onMouseup="stopdrag()"
onSelectStart="return false">
<div align="right" style="background-color:navy">
<img src="layout.png" id="maxname" onClick="maximize()">
<img src="icon_delete.gif" onClick="closeit()">
</div>
<div id="dwindowcontent" style="height:100%"></div>
</div>
<input type="button" value="彈出窗口" onclick='loadwindow(300,200);'>
</body>
</html>
拖曳的實作套路是固定的三步:mousedown 記錄滑鼠與視窗的起始座標並打開 mousemove 監聽、mousemove 依差值更新位置、mouseup 關掉監聽。onSelectStart="return false" 則是為了避免拖曳時把文字選取起來。
這類需求今天已有標準答案:彈窗用 <dialog> 元素(自帶焦點管理與 Escape 關閉),拖曳用 Pointer Events 搭配 setPointerCapture。上面這兩段程式碼的價值在於理解背後的原理——座標差值計算與事件狀態機,換成新 API 時思路完全一致。