做一個像 Windows 視窗一樣能拖動、能縮放的浮層,是前端很值得練手的題目。它涵蓋了滑鼠事件、座標計算、邊界限制與事件捕獲四個核心觀念。
<style>
*{margin:0;padding:0;}
#zhezhao{ /* 拖曳時的透明遮罩,防止選取到文字 */
width:100%; height:100%; background:#f00;
filter:alpha(opacity:0); opacity:0;
z-index:9999; position:absolute; top:0; left:0; display:none;
}
#div2{
width:200px; height:200px; position:relative;
background:#EEEEEE; border:1px solid #f00;
}
#div1{ /* 右下角縮放控制點 */
width:15px; height:15px; background:#99CC00;
position:absolute; right:0; bottom:0;
cursor:nw-resize; z-index:3;
font-size:12px; line-height:15px; text-align:center; color:#FFF;
}
#right{ /* 右邊整條:拉寬 */
width:15px; height:100%; position:absolute; right:0; top:0;
cursor:e-resize; filter:alpha(opacity:0); opacity:0; z-index:1;
}
#bottom{ /* 底部整條:拉高 */
width:100%; height:15px; position:absolute; left:0; bottom:0;
cursor:n-resize; filter:alpha(opacity:0); opacity:0; z-index:1;
}
#div2 p{padding:10px; line-height:24px; font-size:13px;
text-indent:24px; color:#996600;}
#div2 h2{
width:100%; height:25px; line-height:25px; font-size:14px;
background:#CC9900; color:#FFF; text-indent:15px;
cursor:move; overflow:hidden;
}
</style>
整個互動都建立在同一個模式上:mousedown 記錄起點 → mousemove 計算差值 → mouseup 解除綁定。
var mouseStart = {}, divStart = {};
// 拖曳標題列
h2.onmousedown = function (ev) {
var oEvent = ev || event;
mouseStart.x = oEvent.clientX; // 滑鼠起點
mouseStart.y = oEvent.clientY;
divStart.x = oDiv2.offsetLeft; // 浮層起點
divStart.y = oDiv2.offsetTop;
if (h2.setCapture) { // IE 專屬事件捕獲
h2.onmousemove = doDrag3;
h2.onmouseup = stopDrag3;
h2.setCapture();
} else { // 標準做法
document.addEventListener("mousemove", doDrag3, true);
document.addEventListener("mouseup", stopDrag3, true);
}
zhezhao.style.display = 'block'; // 顯示遮罩
};
function doDrag3(ev) {
var oEvent = ev || event;
var l = oEvent.clientX - mouseStart.x + divStart.x; // 新 left
var t = oEvent.clientY - mouseStart.y + divStart.y; // 新 top
// 邊界檢測:不能超出視窗
if (l < 0) l = 0;
else if (l > document.documentElement.clientWidth - oDiv2.offsetWidth)
l = document.documentElement.clientWidth - oDiv2.offsetWidth;
if (t < 0) t = 0;
else if (t > document.documentElement.clientHeight - oDiv2.offsetHeight)
t = document.documentElement.clientHeight - oDiv2.offsetHeight;
oDiv2.style.left = l + "px";
oDiv2.style.top = t + "px";
};
function stopDrag3() {
if (h2.releaseCapture) {
h2.onmousemove = null; h2.onmouseup = null;
h2.releaseCapture();
} else {
document.removeEventListener("mousemove", doDrag3, true);
document.removeEventListener("mouseup", stopDrag3, true);
}
zhezhao.style.display = 'none';
}
拉寬(右邊條)時只改 width,拉高(底部條)時只改 height,右下角控制點則兩者同時改:
function doDrag1(ev) { // 往右拉
var oEvent = ev || event;
var l = oEvent.clientX - mouseStart.x + rightStart.x;
var w = l + oDiv.offsetWidth;
if (w < oDiv.offsetWidth) w = oDiv.offsetWidth; // 不能比最小寬小
else if (w > document.documentElement.clientWidth - oDiv2.offsetLeft)
w = document.documentElement.clientWidth - oDiv2.offsetLeft - 2;
oDiv2.style.width = w + "px";
}
function doDrag(ev) { // 右下角:同時改寬高
var oEvent = ev || event;
var l = oEvent.clientX - mouseStart.x + divStart.x;
var t = oEvent.clientY - mouseStart.y + divStart.y;
var w = l + oDiv.offsetWidth;
var h = t + oDiv.offsetHeight;
if (w < oDiv.offsetWidth) w = oDiv.offsetWidth;
else if (w > document.documentElement.clientWidth - oDiv2.offsetLeft)
w = document.documentElement.clientWidth - oDiv2.offsetLeft - 2;
if (h < oDiv.offsetHeight) h = oDiv.offsetHeight;
else if (h > document.documentElement.clientHeight - oDiv2.offsetTop)
h = document.documentElement.clientHeight - oDiv2.offsetTop - 2;
oDiv2.style.width = w + "px";
oDiv2.style.height = h + "px";
}
拖曳或縮放時顯示 #zhezhao,是為了避免滑鼠移動過程中選取到頁面文字,同時讓 iframe(會吞掉 mousemove)不至于中斷拖曳。這是老派實作的經典手法。
IE 用專屬的 setCapture() 把事件綁定在元素上;標準瀏覽器則在 document 上掛 mousemove。兩條路徑都要寫,才能跨瀏覽器。
每次計算新座標後都要夾住範圍(clamp),否則浮層會被拖出視窗、再也拉不回來。減 2 是為了扣掉 1px 的邊框。
這個範例的邏輯完全正確,但用今天的 API 可以寫得更短:
Pointer Events(pointerdown / pointermove / pointerup)取代滑鼠事件,一次搞定滑鼠、觸控與手寫筆,並用 setPointerCapture() 標準化捕獲行為。transform: translate() 代替 left/top,只觸發合成,不掉幀。h2 上標 role="dialog" 與 aria-label。