讓一個方塊在瀏覽器視窗裡來回彈跳、滑過就停、點叉就關,是早期很常見的漂浮廣告做法。下面分別用原生 JavaScript 與 jQuery 實作一次。
<!DOCTYPE HTML>
<html>
<head>
<title>懸浮視窗 Demo</title>
<script type="text/javascript">
window.onload = function () {
// 建立視窗本體
var oneInner = document.createElement("div");
oneInner.setAttribute("style",
"background:#663398;position:absolute;width:100px;height:100px;" +
"border:solid 3px #2F74A7;cursor:pointer;");
// 建立關閉按鈕
var oneButton = document.createElement("input");
oneButton.setAttribute("type", "button");
oneButton.setAttribute("value", "x");
if (oneButton.style.cssFloat) {
oneButton.style.cssFloat = "right";
} else {
oneButton.style.styleFloat = "right";
}
oneInner.appendChild(oneButton);
document.body.appendChild(oneInner);
// 每 100ms 移動一次
var timer = setInterval(moves, 100);
var x = 10;
var y = 10;
function moves() {
var tops = oneInner.offsetTop;
var lefts = oneInner.offsetLeft;
// 撞到左右邊界就反向
if (lefts >= document.documentElement.clientWidth - oneInner.offsetWidth || lefts <= 0) {
x = -x;
}
// 撞到上下邊界就反向
if (tops >= document.documentElement.clientHeight - oneInner.offsetHeight || tops <= 0) {
y = -y;
}
tops += y;
lefts += x;
oneInner.style.top = tops + "px";
oneInner.style.left = lefts + "px";
}
// 滑鼠移入暫停
oneInner.onmouseover = function () {
clearInterval(timer);
};
// 滑鼠移出繼續
oneInner.onmouseout = function () {
timer = setInterval(moves, 100);
};
// 點擊關閉
oneButton.onclick = function () {
document.body.removeChild(oneInner);
};
};
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>懸浮視窗 Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jquery.min.js"></script>
<script>
$(function () {
// 建立視窗
$("<div/>", { id: "moveWindow" })
.css({ width: "200px", height: "200px", border: "solid 3px #2F74A7",
background: "#663398", position: "absolute", cursor: "pointer" })
.appendTo("body");
// 建立關閉按鈕
$("<div/>", { id: "removeMW" })
.css({ width: "20px", height: "20px", background: "red", float: "right" })
.appendTo("#moveWindow");
var move = setInterval(moves, 100);
var x = 10;
var y = 10;
function moves() {
var mw = $("#moveWindow").offset();
var lefts = mw.left;
var tops = mw.top;
if (lefts >= $(window).width() - $("#moveWindow").width() || lefts <= 0) { x = -x; }
if (tops >= $(window).height() - $("#moveWindow").height() || tops <= 0) { y = -y; }
lefts += x;
tops += y;
$("#moveWindow").offset({ top: tops, left: lefts });
}
$("#moveWindow").mouseover(function () { clearInterval(move); });
$("#moveWindow").mouseout(function () { move = setInterval(moves, 100); });
$("#removeMW").click(function () { $("#moveWindow").remove(); });
});
</script>
</head>
<body>
</body>
</html>
1. 頁面載入完成後,動態插入視窗元素,再往視窗裡塞一個關閉按鈕。
2. 用 setInterval 週期性呼叫 moves 函式驅動動畫。
3. moves 先讀取視窗目前的座標,每次位移固定步長;一旦貼到瀏覽器邊緣就把速度值取負號,方向隨之反轉。
4. 再掛上三個事件:滑鼠移入停止、移出恢復、點擊按鈕移除元素。
坦白說,這個特效不該出現在正式專案裡。除了干擾閱讀,它還有幾個實際毛病:setInterval 間隔固定會造成位移跳動而非平滑動畫、沒有清除計時器的機制容易在單頁應用中累積、閃爍的視窗對注意力障礙或視覺敏感的使用者並不友善。若今天的需求是「做一個可拖曳的浮動元件」,請改用 requestAnimationFrame 搭配 transform 做位移,並加上 prefers-reduced-motion 的判斷;若只是想推播訊息,用 position: fixed 的卡片加上進場動畫會體面得多。
把它當成理解 offsetLeft、offsetTop 與邊界判斷的練習題,價值比當成功能來得高。