這是一個自製的浮動視窗元件:不依賴任何框架,用原生 JS 動態組出視窗的 DOM,支援拖曳移動、右下角縮放、雙擊標題列最大化/還原、摺疊成只剩標題列、關閉,以及多視窗的 z-index 管理。
<style type="text/css">
.divWindow { word-wrap: break-word; position: absolute; overflow: hidden; }
.divBar {
border: #000 1px solid; position: absolute; border-bottom: #000 1px solid;
width: 100%; height: 20px; background-color: #0099FF;
cursor: hand; line-height: 20px;
}
.divChange { position: absolute; right: 25px; font-size: 10pt; }
.divClose { position: absolute; right: 5px; font-size: 11pt; }
.divTitle {
position: absolute; left: 5px; font-size: 10pt; white-space: nowrap;
text-overflow: ellipsis; -o-text-overflow: ellipsis; overflow: hidden;
}
.divContent {
border: #000 1px solid; border-top: #000 0px solid;
position: absolute; top: 20px; width: 100%;
background-color: #FFFFFF; overflow-y: auto;
}
.divReSize {
height: 7px; width: 7px; overflow: hidden; background-color: #0000FF;
position: absolute; bottom: 6px; right: 6px; cursor: nw-resize;
}
.divIframe { height: 100%; width: 100%; }
</style>
<script language="javascript">
var zindex = 0; // 全域計數器,用來決定誰在上面
function dragClass(name, title, content, left, top, width, height) {
var isMouseDown = false;
var maximum = false;
var offX = 0, offY = 0; // 抓取點與視窗左上角的偏移
var oldLeft, oldTop; // 還原最大化前的位置
// 按下標題列:記錄偏移並提高層級
this.mousedown = function () {
Bar.setCapture();
offX = parseInt(event.clientX) - parseInt(Window.style.left);
offY = parseInt(event.clientY) - parseInt(Window.style.top);
isMouseDown = true;
if (Window.style.zIndex <= zindex) { Window.style.zIndex = ++zindex; }
};
// 拖曳中:更新位置
this.mousemove = function () {
if (isMouseDown && !maximum) {
Bar.style.cursor = 'move';
Window.style.left = event.clientX - offX;
Window.style.top = event.clientY - offY;
if (Window.style.zIndex <= zindex) { Window.style.zIndex = ++zindex; }
}
};
// 放開:解除抓取,並把超出畫面的視窗拉回來
this.mouseup = function () {
Bar.releaseCapture();
Bar.style.cursor = 'hand';
if (parseInt(Window.style.top) < 0) { Window.style.top = '0px'; }
if (parseInt(Window.style.left) < 0) { Window.style.left = '0px'; }
isMouseDown = false;
};
// 雙擊標題列:最大化/還原
this.dblclick = function () {
if (!maximum) {
oldLeft = Window.style.left;
oldTop = Window.style.top;
Window.style.left = '0px';
Window.style.top = '0px';
Window.style.width = document.body.clientWidth;
Title.style.width = (document.body.clientWidth - 40) + 'px';
ReSize.style.display = 'none';
if (Change.innerText == '-') {
Window.style.height = '100%';
Content.style.height = document.body.clientHeight - 20;
} else {
Window.style.height = '20px';
}
maximum = true;
} else {
Window.style.left = oldLeft;
Window.style.top = oldTop;
Window.style.width = width + 'px';
Title.style.width = (width - 40) + 'px';
ReSize.style.display = '';
if (Change.innerText == '-') {
Window.style.height = height + 'px';
Content.style.height = parseInt(height - 20) + 'px';
} else {
Window.style.height = '20px';
}
maximum = false;
}
if (Window.style.zIndex <= zindex) { Window.style.zIndex = ++zindex; }
};
// 摺疊/展開
this.changeWindow = function () {
event.cancelBubble = true; // 避免觸發視窗整體的 click
if (Change.innerText == '-') {
Window.style.height = '20px';
Change.innerText = '□';
Content.style.display = 'none';
ReSize.style.display = 'none';
} else {
if (maximum) {
Window.style.height = '100%';
Content.style.height = document.body.clientHeight - 20;
} else {
Window.style.height = height + 'px';
Content.style.height = parseInt(height - 20) + 'px';
}
Content.style.display = '';
ReSize.style.display = '';
Change.innerText = '-';
}
};
接著依序建立視窗本體、標題列、標題文字、摺疊鈕、關閉鈕、內容區、縮放把手與 iframe:
var Window = document.createElement("div");
Window.id = "divWindow" + name;
Window.className = "divWindow";
Window.style.left = left + 'px';
Window.style.top = top + 'px';
Window.style.width = width + 'px';
Window.style.height = height + 'px';
Window.onclick = function () {
if (parseInt(Window.style.zIndex) <= zindex) { Window.style.zIndex = ++zindex; }
};
this.Window = Window; // 公開為屬性,外部可取得視窗節點
var Bar = document.createElement("div");
Bar.id = "divBar" + name;
Bar.onselectstart = "return false"; // 避免拖曳時選到文字
Bar.className = "divBar";
Bar.onmousedown = this.mousedown;
Bar.ondblclick = this.dblclick;
Bar.onmousemove = this.mousemove;
Bar.onmouseup = this.mouseup;
Window.appendChild(Bar);
var Title = document.createElement("span");
Title.className = "divTitle";
Title.style.width = (width - 40) + 'px';
Title.innerText = title;
Bar.appendChild(Title);
var Change = document.createElement("span");
Change.className = "divChange";
Change.innerText = "-";
Change.ondblclick = this.changeWindow;
Change.onclick = this.changeWindow;
Bar.appendChild(Change);
var Close = document.createElement("span");
Close.className = "divClose";
Close.innerText = "×";
Close.onclick = function () { Window.style.display = 'none'; };
Bar.appendChild(Close);
var Content = document.createElement("div");
Content.id = "divContent" + name;
Content.className = "divContent";
Content.innerHTML = content;
Content.style.height = parseInt(height - 20) + 'px';
Window.appendChild(Content);
var ReSize = document.createElement("div");
ReSize.className = "divReSize";
ReSize.onmousedown = function () {
if (Window.style.zIndex <= zindex) { Window.style.zIndex = ++zindex; }
ReSize.setCapture();
isMouseDown = true;
};
ReSize.onmousemove = function () {
if (isMouseDown && !maximum) {
var w = parseInt(event.clientX) - parseInt(Window.style.left) + 5;
var h = parseInt(event.clientY) - parseInt(Window.style.top) + 5;
if (w > 100) { // 最小寬度限制
Window.style.width = w + 'px';
Title.style.width = (w - 40) + 'px';
}
if (h > 100) { // 最小高度限制
Window.style.height = h + 'px';
Content.style.height = parseInt(h - 20) + 'px';
}
}
};
ReSize.onmouseup = function () { ReSize.releaseCapture(); isMouseDown = false; };
Window.appendChild(ReSize);
// 額外鋪一層 iframe:舊版 IE 裡用來遮住 select 下拉選單
var Iframe = document.createElement("iframe");
Iframe.className = "divIframe";
Window.appendChild(Iframe);
document.body.appendChild(Window);
}
</script>
<script>
// dragClass(ID, 視窗標題, 內容, X座標, Y座標, 寬, 高)
var c1 = "窗口1 內容文字……";
objWin1 = new dragClass('win1', '拖動窗口1', c1, 0, 150, 300, 300);
var c2 = "窗口2 內容文字……";
objWin2 = new dragClass('win2', '拖動窗口2', c2, 350, 150, 300, 300);
var objWin3;
function openWin() {
if (objWin3 == null) {
var c3 = "123 窗口3 內容文字……";
objWin3 = new dragClass('win3', c3, c3, 700, 150, 300, 300);
} else {
if (objWin3.Window.style.display == 'none') {
objWin3.Window.style.display = ''; // 已關閉過就重新顯示
}
}
}
</script>
<input type="button" value="彈出【窗口3】" onClick="openWin()" />
setCapture 是拖曳的關鍵。滑鼠在快速移動時可能瞬間移出元素範圍,若沒有 pointer capture,mousemove 就會斷掉、視窗拖到一半卡住。setCapture 讓元素在按鍵放開前持續收到所有滑鼠事件,這正是這個元件能拖得順的原因。
offX / offY 避免視窗跳動。如果直接用 mouseX 當視窗座標,視窗左上角會瞬間跳到滑鼠位置。減去抓取點的偏移量,才能保持「你從哪裡抓起來就從哪裡跟著走」的手感。
zindex 全域遞增。每次互動就把自己的層級設成比目前最大值還大,於是「最後被操作的視窗永遠在最上層」,這是視窗管理最省事的做法。
摺疊鈕要 stopPropagation。changeWindow 裡的 event.cancelBubble = true 是為了避免點擊被視窗本體的 onclick 攔走而觸發額外的層級提升。標準寫法應改用 event.stopPropagation()。
iframe 墊層。舊版 IE 的 select 是系統級視窗元件,會穿透一般的 div 蓋在上面。塞一個 iframe 當底襯是當年的標準解法;現代瀏覽器沒有這個問題,可以直接移除。
第一,改用標準事件 API。範例大量依賴 setCapture、releaseCapture、event.cancelBubble、全域 event 物件等 IE 專屬寫法。現在一律改用 pointerdown/pointermove/pointerup 搭配 setPointerCapture,並把 event 當成參數傳入,跨瀏覽器都能跑。
第二,用 CSS transform 做位移。反覆改 style.left/top 會觸發重排;改用 transform: translate(x, y) 只走合成,效能好得多。
第三,位置與尺寸改用 transform 與 CSS 變數管理,並在視窗 resize 時把超出範圍的視窗拉回可視區域,避免視窗被拖到螢幕外救不回來。
第四,加上鍵盤操作與 ARIA 屬性(role="dialog"、aria-modal、Esc 關閉),讓它對螢幕閱讀器與只用鍵盤的使用者也友善。
儘管有這些可以改進的地方,這個元件把「浮動視窗需要處理哪些狀態」示範得很完整,作為自製 UI 元件的入門練習非常合適。