DIV CSS 佈局教程網

JavaScript 實作可拖曳、拖動變色、可關閉的浮動視窗
編輯:JavaScript綜合知識     

這是一個以原生 JavaScript 加 CSS 打造的浮動視窗元件。與一般拖曳窗不同的地方在於:拖動過程中標題欄與邊框會變色,讓使用者清楚知道「目前抓的是哪一個視窗」;同時支援最小化、顯示/隱藏與關閉。

拖曳三兄弟

<script language="JScript">
var x0 = 0, y0 = 0, x1 = 0, y1 = 0;   // 滑鼠起點與視窗原位置
var offx = 6, offy = 6;               // 陰影層的偏移量
var moveable = false;
var hover = 'orange', normal = '#336699';
var index = 10000;                    // 用來遞增 z-index

// 開始拖動
function startDrag(obj) {
    if (event.button == 1) {
        obj.setCapture();                       // 鎖定滑鼠事件到標題欄
        var win = obj.parentNode;
        var sha = win.nextSibling;              // 陰影層
        x0 = event.clientX; y0 = event.clientY;
        x1 = parseInt(win.style.left); y1 = parseInt(win.style.top);
        normal = obj.style.backgroundColor;     // 記住原色,停止時才能還原
        obj.style.backgroundColor = hover;      // 變色提示
        win.style.borderColor = hover;
        obj.nextSibling.style.color = hover;
        sha.style.left = x1 + offx;
        sha.style.top = y1 + offy;
        moveable = true;
    }
}

// 拖動中
function drag(obj) {
    if (moveable) {
        var win = obj.parentNode;
        var sha = win.nextSibling;
        win.style.left = x1 + event.clientX - x0;
        win.style.top = y1 + event.clientY - y0;
        sha.style.left = parseInt(win.style.left) + offx;   // 陰影跟著走
        sha.style.top = parseInt(win.style.top) + offy;
    }
}

// 停止拖動
function stopDrag(obj) {
    if (moveable) {
        var win = obj.parentNode;
        var sha = win.nextSibling;
        win.style.borderColor = normal;
        obj.style.backgroundColor = normal;
        obj.nextSibling.style.color = normal;
        sha.style.left = obj.parentNode.style.left;   // 陰影歸位
        sha.style.top = obj.parentNode.style.top;
        obj.releaseCapture();
        moveable = false;
    }
}

// 取得焦點:點擊時把 z-index 拉到最上
function getFocus(obj) {
    if (obj.style.zIndex != index) {
        index = index + 2;
        obj.style.zIndex = index;
        obj.nextSibling.style.zIndex = index - 1;   // 陰影永遠在視窗下一層
    }
}

拖曳的標準三步驟就是 mousedown 記錄初始座標、mousemove 計算位移、mouseup 收尾。這裡靠 setCapture() 把滑鼠事件鎖定在標題欄上,避免游標移太快時跑到視窗外就「掉標」——這是 IE 提供的機制,現代做法是改用 document 上派生物的 mousemove/mouseup 監聽。

最小化

function min(obj) {
    var win = obj.parentNode.parentNode;
    var sha = win.nextSibling;
    var tit = obj.parentNode;      // 標題列
    var msg = tit.nextSibling;     // 內容區
    var flg = msg.style.display == "none";
    if (flg) {   // 目前是收起的,展開
        win.style.height = parseInt(msg.style.height) + parseInt(tit.style.height) + 2 * 2;
        sha.style.height = win.style.height;
        msg.style.display = "block";
        obj.innerHTML = "0";
    } else {     // 收起
        win.style.height = parseInt(tit.style.height) + 2 * 2;
        sha.style.height = win.style.height;
        obj.innerHTML = "2";
        msg.style.display = "none";
    }
}

以建構式產生視窗

把視窗包成建構式,就能像 new 物件一樣一次開好幾個:

function xWin(id, w, h, l, t, tit, msg) {
    index = index + 2;
    this.id = id; this.width = w; this.height = h;
    this.left = l; this.top = t;
    this.zIndex = index; this.title = tit; this.message = msg;
    this.bulid = bulid;
    this.bulid();
}

function bulid() {
    var str = "<div id=xMsg" + this.id + " style='z-index:" + this.zIndex +
        ";width:" + this.width + ";height:" + this.height +
        ";left:" + this.left + ";top:" + this.top +
        ";background-color:" + normal + ";color:" + normal +
        ";font-size:8pt;font-family:Tahoma;position:absolute;cursor:default;" +
        "border:2px solid " + normal + ";' onmousedown='getFocus(this)'>" +
        "<div style='background-color:" + normal + ";width:" + (this.width - 4) +
        ";height:20;color:white;' onmousedown='startDrag(this)' onmouseup='stopDrag(this)'" +
        " onmousemove='drag(this)' ondblclick='min(this.childNodes[1])'>" +
        "<span style='width:" + (this.width - 28) + ";padding-left:3px;'>" + this.title + "</span>" +
        "<span style='width:12;border-width:0px;color:white;font-family:webdings;' onclick='min(this)'>0</span>" +
        "<span style='width:12;border-width:0px;color:white;font-family:webdings;' onclick='ShowHide(\"" + this.id + "\",null)'>r</span>" +
        "</div>" +
        "<div style='width:100%;height:" + (this.height - 24) + ";background-color:white;" +
        "line-height:14px;word-break:break-all;padding:3px;'>" + this.message + "</div>" +
        "</div>" +
        // 半透明陰影層
        "<div id=xMsg" + this.id + "bg style='width:" + this.width + ";height:" + this.height +
        ";top:" + this.top + ";left:" + this.left + ";z-index:" + (this.zIndex - 1) +
        ";position:absolute;background-color:black;filter:alpha(opacity=40);'></div>";
    document.body.insertAdjacentHTML("beforeEnd", str);
}

// 顯示 / 隱藏
function ShowHide(id, dis) {
    var bdisplay = (dis == null)
        ? ((document.getElementById("xMsg" + id).style.display == "") ? "none" : "")
        : dis;
    document.getElementById("xMsg" + id).style.display = bdisplay;
    document.getElementById("xMsg" + id + "bg").style.display = bdisplay;
}

function initialize() {
    new xWin("1", 160, 200, 200, 200, "窗口1", "xWin Demo");
    new xWin("2", 240, 200, 100, 100, "窗口2", "Welcome");
    new xWin("3", 200, 160, 250,  50, "窗口3", "Copyright");
    ShowHide("1", "none");   // 預設隱藏第一個
}
window.onload = initialize;
</script>

頁面用三個連結控制顯示:

<body onselectstart="return false" oncontextmenu="return false">
 <a onclick="ShowHide('1',null);return false;" href="">Windows 1</a>
 <a onclick="ShowHide('2',null);return false;" href="">Windows 2</a>
 <a onclick="ShowHide('3',null);return false;" href="">Windows 3</a>
</body>

這份 2008 年前後風格的程式有幾處是時代的產物,讀時可以對照理解:document.all、setCapture、insertAdjacentHTML 的 IE 專屬用法,以及用 webdings 字型把字母 0/r 渲染成圖示的小聰明。若要在今天重寫,會改成:以 position: fixed 定位、用 pointer events 統一處理滑鼠與觸控、陰影改用 box-shadow、圓角用 border-radius,並以 class 切換取代直接改 style。核心的座標差值計算則完全適用。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved