DIV CSS 佈局教程網

JavaScript 實現動畫展開的半透明提示層(含拖曳)
編輯:關於JavaScript     

這是一個以 JavaScript 逐步改變寬高、製造「展開」動畫的提示層,同時支援滑鼠拖曳,拖曳過程中會降低不透明度以示區別。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DOM 半透明提示層</title>
<style type="text/css">
body,span,div,td{font-size:12px;line-height:1.5em;color:#849BCA;}
#bodyL{float:left;width:84px;margin-right:2px;}
a.od{width:80px;height:25px;line-height:25px;text-align:center;font-weight:bold;
     border:2px solid #849BCA;display:block;color:#547BC9;float:left;
     text-decoration:none;margin-top:2px;}
a.od:link, a.od:visited{background:#EEF1F8;}
a.od:hover, a.od:active{background:#EEE;}
#fd{width:500px;height:200px;background:#EDF1F8;border:2px solid #849BCA;
    margin-top:2px;margin-left:2px;overflow:hidden;position:absolute;
    left:0;top:0;cursor:move;}
.content{padding:10px;}
</style>
</head>
<body>
<div id="bodyL">
  <a href="#" class="od" onclick="show('fd');return false;">[打開層]</a>
  <a href="#" class="od" onclick="closeed('fd');return false;">[關閉層]</a>
</div>
<div id="fd" style="display:none;filter:alpha(opacity=100);opacity:1;">
  <div class="content">移動層</div>
</div>

<script type="text/javascript">
var prox, proy, proxc, proyc;   // 四個計時器:展開 x/y、收起 x/y

function show(id) {
    clearInterval(prox); clearInterval(proy);
    clearInterval(proxc); clearInterval(proyc);
    var o = document.getElementById(id);
    o.style.display = "block";
    o.style.width = "1px";
    o.style.height = "1px";
    prox = setInterval(function () { openx(o, 500); }, 10);
}

function openx(o, x) {
    var cx = parseInt(o.style.width);
    if (cx < x) {
        o.style.width = (cx + Math.ceil((x - cx) / 5)) + "px";
    } else {
        clearInterval(prox);
        proy = setInterval(function () { openy(o, 200); }, 10);
    }
}

function openy(o, y) {
    var cy = parseInt(o.style.height);
    if (cy < y) {
        o.style.height = (cy + Math.ceil((y - cy) / 5)) + "px";
    } else {
        clearInterval(proy);
    }
}

function closeed(id) {
    clearInterval(prox); clearInterval(proy);
    clearInterval(proxc); clearInterval(proyc);
    var o = document.getElementById(id);
    if (o.style.display == "block") {
        proyc = setInterval(function () { closey(o); }, 10);
    }
}

function closey(o) {
    var cy = parseInt(o.style.height);
    if (cy > 0) {
        o.style.height = (cy - Math.ceil(cy / 5)) + "px";
    } else {
        clearInterval(proyc);
        proxc = setInterval(function () { closex(o); }, 10);
    }
}

function closex(o) {
    var cx = parseInt(o.style.width);
    if (cx > 0) {
        o.style.width = (cx - Math.ceil(cx / 5)) + "px";
    } else {
        clearInterval(proxc);
        o.style.display = "none";
    }
}

/* ---------- 滑鼠拖曳 ---------- */
var od = document.getElementById("fd");
var dx, dy, mx, my, mouseD;
var odrag;
var isIE = document.all ? true : false;

document.onmousedown = function (e) {
    var e = e ? e : event;
    if (e.button == (document.all ? 1 : 0)) { mouseD = true; }
};

document.onmouseup = function () {
    mouseD = false;
    odrag = "";
    if (isIE) {
        od.releaseCapture();
        od.filters.alpha.opacity = 100;
    } else {
        window.releaseEvents(od.MOUSEMOVE);
        od.style.opacity = 1;
    }
};

od.onmousedown = function (e) {
    odrag = this;
    var e = e ? e : event;
    if (e.button == (document.all ? 1 : 0)) {
        mx = e.clientX; my = e.clientY;
        od.style.left = od.offsetLeft + "px";
        od.style.top = od.offsetTop + "px";
        if (isIE) {
            od.setCapture();
            od.filters.alpha.opacity = 50;   // 拖曳時半透明
        } else {
            window.captureEvents(Event.MOUSEMOVE);
            od.style.opacity = 0.5;
        }
    }
};

document.onmousemove = function (e) {
    var e = e ? e : event;
    if (mouseD == true && odrag) {
        var mrx = e.clientX - mx;
        var mry = e.clientY - my;
        od.style.left = (parseInt(od.style.left) + mrx) + "px";
        od.style.top = (parseInt(od.style.top) + mry) + "px";
        mx = e.clientX; my = e.clientY;
    }
};
</script>
</body>
</html>

動畫部分的思路是「先展開寬度,再展開高度」,收合時順序相反;每一步的增量用 Math.ceil((目標 - 當前) / 5) 計算,形成先快後慢的減速效果,比固定步長自然。

拖曳部分的關鍵是 mx/my 記錄「上一幀的滑鼠位置」,每移動一次就更新一次,如此即使滑鼠移出視窗再回來也不會跳位。拖曳期間把不透明度降到 0.5,是很常見的視覺回饋手法。

這份程式碼的年代痕跡相當明顯:document.all、setCapture、window.captureEvents、od.filters.alpha.opacity 都是 IE 專屬 API,而 document.onmousedown 這種全域指派方式在多元件並存時會互相覆蓋。現代改寫會短得多:

const fd = document.getElementById("fd");

// 展開動畫交給 CSS transition
function show() {
    fd.style.display = "block";
    requestAnimationFrame(() => {
        fd.style.transition = "width .3s ease, height .3s ease";
        fd.style.width = "500px";
        fd.style.height = "200px";
    });
}

// 拖曳改用 Pointer Events
fd.addEventListener("pointerdown", (e) => {
    const startX = e.clientX, startY = e.clientY;
    const left = fd.offsetLeft, top = fd.offsetTop;
    fd.setPointerCapture(e.pointerId);
    fd.style.opacity = 0.5;

    const onMove = (ev) => {
        fd.style.left = left + ev.clientX - startX + "px";
        fd.style.top = top + ev.clientY - startY + "px";
    };
    const onUp = () => {
        fd.style.opacity = 1;
        fd.removeEventListener("pointermove", onMove);
        fd.removeEventListener("pointerup", onUp);
    };
    fd.addEventListener("pointermove", onMove);
    fd.addEventListener("pointerup", onUp);
});

用 Pointer Events 的好處是滑鼠、觸控與手寫筆都走同一套程式碼,行動裝置上也能正常拖曳——這是舊版滑鼠事件無法做到的。

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