公告欄、跑馬燈常需要文字連續滾動且不中斷。核心技巧是把內容複製一份接在后面,滾到尾端時瞬間跳回頭,視覺上就完全接軌。以下四個方向的做法互通,只在「用 scrollTop 還是 scrollLeft」以及正反向的處理上有差異。
一個可捲動的外層容器 marquee,一個隱藏的樣板層 templayer 用來量測內容高度或寬度,再用 setInterval 週期性推動捲動位置。滑鼠移入時暫停、移出時恢復,是必備的體貼設計。
<div id="marquees">
<a href="#">鏈接一</a><br><br>
<a href="#">鏈接二</a><br><br>
<a href="#">鏈接三</a><br><br>
<a href="#">鏈接四</a><br><br>
</div>>
<script language="JavaScript">
marqueesHeight = 200;
stopscroll = false;
with (marquees) {
style.width = 0;
style.height = marqueesHeight;
style.overflowX = "visible";
style.overflowY = "hidden";
noWrap = true;
onmouseover = new Function("stopscroll=true");
onmouseout = new Function("stopscroll=false");
}
document.write('<div id="templayer"
style="position:absolute;z-index:1;visibility:hidden"></div>');
preTop = 0;
currentTop = 0;
function init() {
templayer.innerHTML = "";
// 反覆複製直到樣板層高過容器,確保捲到底前不會露出空白
while (templayer.offsetHeight < marqueesHeight) {
templayer.innerHTML += marquees.innerHTML;
}
// 內容直接加倍,接縫處自然銜接
marquees.innerHTML = templayer.innerHTML + templayer.innerHTML;
setInterval("scrollUp()", 100);
}
document.body.onload = init;
function scrollUp() {
if (stopscroll == true) return;
preTop = marquees.scrollTop;
marquees.scrollTop += 1;
if (preTop == marquees.scrollTop) {
// 已經到底(瀏覽器不再增加),瞬間跳回接近尾端的位置
marquees.scrollTop = templayer.offsetHeight - marqueesHeight;
marquees.scrollTop += 1;
}
}
</script>
向下要從尾端往頭端走,因此 init 裡是把樣板層附加在後面,並先將捲動位置設到一個較大的值,再逐步遞減。這裡多了一個 getlimit 變數用來記住起始上限:
marqueesHeight = 200;
with (marquees) {
style.width = 0;
style.height = marqueesHeight;
style.overflowX = "visible";
style.overflowY = "hidden";
noWrap = true;
onmouseover = new Function("stopscroll=true");
onmouseout = new Function("stopscroll=false");
}
document.write('<div id="templayer"
style="position:absolute;z-index:1;visibility:hidden"></div>');
preTop = 0; currentTop = 0; getlimit = 0; stopscroll = false;
function init() {
templayer.innerHTML = "";
while (templayer.offsetHeight < marqueesHeight) {
templayer.innerHTML += marquees.innerHTML;
}
marquees.innerHTML += templayer.innerHTML;
setInterval("scrollDown()", 10);
}
init();
function scrollDown() {
if (stopscroll == true) return;
preTop = marquees.scrollTop;
marquees.scrollTop -= 1;
if (preTop == marquees.scrollTop) {
if (!getlimit) {
marquees.scrollTop = templayer.offsetHeight * 2;
getlimit = marquees.scrollTop;
}
marquees.scrollTop = getlimit - templayer.offsetHeight + marqueesHeight;
marquees.scrollTop -= 1;
}
}
水平方向把 height/width 對調、overflowX 改 hidden,並改用 scrollLeft:
<div id="marquees">
<a href="#">鏈接一</a> <a href="#">鏈接二</a>
<a href="#">鏈接三</a> <a href="#">鏈接四</a>
</div>
<div id="templayer"
style="position:absolute;left:0;top:0;visibility:hidden"></div>
<script language="JavaScript">
marqueesWidth = 200;
with (marquees) {
style.height = 0;
style.width = marqueesWidth;
style.overflowX = "hidden";
style.overflowY = "visible";
noWrap = true;
onmouseover = new Function("stopscroll=true");
onmouseout = new Function("stopscroll=false");
}
preLeft = 0; currentLeft = 0; stopscroll = false;
function init() {
templayer.innerHTML = "";
while (templayer.offsetWidth < marqueesWidth) {
templayer.innerHTML += marquees.innerHTML;
}
marquees.innerHTML += templayer.innerHTML;
setInterval("scrollLeft()", 100);
}
init();
function scrollLeft() {
if (stopscroll == true) return;
preLeft = marquees.scrollLeft;
marquees.scrollLeft += 1;
if (preLeft == marquees.scrollLeft) {
marquees.scrollLeft = templayer.offsetWidth - marqueesWidth + 1;
}
}
</script>
marqueesWidth = 200;
with (marquees) {
style.height = 0;
style.width = marqueesWidth;
style.overflowX = "hidden";
style.overflowY = "visible";
noWrap = true;
onmouseover = new Function("stopscroll=true");
onmouseout = new Function("stopscroll=false");
}
preTop = 0; currentTop = 0; getlimit = 0; stopscroll = false;
function init() {
templayer.innerHTML = "";
while (templayer.offsetWidth < marqueesWidth) {
templayer.innerHTML += marquees.innerHTML;
}
marquees.innerHTML += templayer.innerHTML;
setInterval("scrollRight()", 10);
}
init();
function scrollRight() {
if (stopscroll == true) return;
preLeft = marquees.scrollLeft;
marquees.scrollLeft -= 1;
if (preLeft == marquees.scrollLeft) {
if (!getlimit) {
marquees.scrollLeft = templayer.offsetWidth * 2;
getlimit = marquees.scrollLeft;
}
marquees.scrollLeft = getlimit - templayer.offsetWidth + marqueesWidth;
marquees.scrollLeft -= 1;
}
}
不論方向,判斷「是否該回到頭」的方式都是比較 scrollTop/scrollLeft 有沒有變動。當值已經在邊界而無法再增加(或減少)時,前後兩次讀到的值會相同,這就是重置的時機。這比去計算總高度再取模數來得單純,也不必依賴 offsetHeight 的準確性。
另一個關鍵是 templayer.innerHTML 的 while 迴圈:先複製到「樣板層超過容器尺寸」,再把樣板層內容加倍塞回容器。如此一來,任何時刻捲動區塊內的內容都遠大於可視範圍,接縫永遠落在畫面之外。
這套寫法用了幾個已不建議的語法:document.write 在事件或載入後呼叫會清空整份文件,with 在嚴格模式已禁用,new Function 相當於 eval 有安全疑慮。今日更合適的選擇是:
@keyframes 搭配 transform: translateX() 做動畫,效能最好,還能靠 :hover { animation-play-state: paused; } 實作滑鼠暫停。requestAnimationFrame 取代 setInterval,並以 transform 取代 scrollLeft,避免觸發重排。另外,HTML5 的 <marquee> 標籤雖看似方便,但早已被標準淘汰,不應再用於新專案。