本文示範一個在垂直方向來回漂浮的浮動層,常用於公告欄或促銷訊息。程式內把可調參數集中在前端,方便自行調整位置、速度與步調。
做法並不玄妙:讓浮動層的 top 值持續位移,碰到上邊界就改往下走,碰到下邊界就改往上走,再用 setTimeout 週期性呼叫自己,形成往返。使用者可設定的參數有三個:
• XX:浮動層的 X 座標,也就是左邊距;
• xstep:每次移動的像素數,數值越小看起來越平順(最小值為 1);
• delay_time:兩步之間的間隔毫秒數,越小移動越快。
邊界判斷需要同時知道「視窗高度」與「浮動層自身高度」,前者取 clientHeight 或 innerHeight,後者取 offsetHeight,兩相減就是 top 的下限。
運行效果截圖如下:

線上演示:
https://demo.jb51.net/js/2015/js-up-down-float-move-win-codes/
<HTML>
<HEAD>
<TITLE>上下浮動的窗口</TITLE>
<style type="text/css">
a:hover{color:#00ff00}
a{color:#000000;text-decoration:none}
</style>
</HEAD>
<BODY>
<div id="floatpoint" style="position:absolute;visibility:visible;">
<!-- 兩個反白處之間放上你要顯示的內容 -->
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td align="center" bgcolor="#E3E3E3"><font color="#000000">最新消息!</font></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" style="font-size:9pt;"><a href="#" target="_blank"></a></td>
</tr>
</table>
</div>
<script LANGUAGE="JavaScript1.2">
// ===== 使用者自訂區 =====
var XX = 10; // 浮動層的 X 坐標,即左邊距
var xstep = 1; // 移動步長,越小越平滑,最小值為 1
var delay_time = 60; // 每步的時間間隔,越小移動越快
// ========================
var YY = 0, ch = 0, oh = 0, yon = 0;
var ns4 = document.layers ? 1 : 0;
var ie = document.all ? 1 : 0;
var ns6 = document.getElementById && !document.all ? 1 : 0;
if (ie) {
YY = document.body.clientHeight;
floatpoint.style.top = YY;
} else if (ns4) {
YY = window.innerHeight;
document.floatpoint.pageY = YY;
document.floatpoint.visibility = "hidden";
} else if (ns6) {
YY = window.innerHeight;
document.getElementById('floatpoint').style.top = YY;
}
function reloc1(){
if (yon == 0) { YY = YY - xstep; } else { YY = YY + xstep; }
if (ie) {
ch = document.body.clientHeight;
oh = floatpoint.offsetHeight;
} else if (ns4) {
ch = window.innerHeight;
oh = document.floatpoint.clip.height;
} else if (ns6) {
ch = window.innerHeight;
oh = document.getElementById("floatpoint").offsetHeight;
}
// 超出上界則反向
if (YY < 0) { yon = 1; YY = 0; }
// 超出下界則反向
if (YY >= (ch - oh)) { yon = 0; YY = (ch - oh); }
if (ie) {
floatpoint.style.left = XX;
floatpoint.style.top = YY + document.body.scrollTop;
} else if (ns4) {
document.floatpoint.pageX = XX;
document.floatpoint.pageY = YY + window.pageYOffset;
} else if (ns6) {
document.getElementById("floatpoint").style.left = XX;
document.getElementById("floatpoint").style.top = YY + window.pageYOffset;
}
}
function onad(){
if (ns4) document.floatpoint.visibility = "visible";
loopfunc();
}
function loopfunc(){
reloc1();
setTimeout('loopfunc()', delay_time);
}
if (ie || ns4 || ns6) window.onload = onad;
</script>
</BODY>
</HTML>
這份程式碼寫於 IE6 與 Netscape 4 還在服役的年代,因此充滿了 document.layers、document.all 這類分支。放到今天,那些判斷都可以刪掉,改用標準 API 會短很多:
const box = document.getElementById('floatpoint');
let pos = window.innerHeight, dir = 1, step = 1;
function tick() {
const max = window.innerHeight - box.offsetHeight;
pos += step * dir;
if (pos <= 0) { pos = 0; dir = 1; }
if (pos >= max) { pos = max; dir = -1; }
box.style.transform = `translateY(${pos}px)`;
requestAnimationFrame(tick);
}
requestAnimationFrame(tick);
三個關鍵升級:用 requestAnimationFrame 取代 setTimeout,讓動畫與螢幕刷新同步、分頁隱藏時自動暫停,也省電;用 transform 取代 top,只觸發合成而不觸發重排,效能好得多;把 setTimeout('loopfunc()') 的字串形式換成函式參照,避免一次不必要的 eval。
另外提醒,這種主動漂浮的浮動層在行動裝置上很容易擋住內容、妨礙閱讀,多數網站已經改為固定在版面某處或折疊成一個小按鈕。使用前建議先想清楚它是否真的帶來價值,並務必提供關閉入口。
希望本文所述對大家的 JavaScript 設計有所幫助。