近期專案需要上下左右皆可無縫循環的滾動效果。以下示範一個「一行多列、逐行循環、帶停頓」的公告列表,重點在如何讓內容接龍後 seamlessly 回到起點。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd"> <HTML xmlns="https://www.w3.org/1999/xhtml"> <HEAD> <TITLE>一行多列文字循環滾動帶停頓-</TITLE> <meta name="keywords" content="網頁特效" /> <meta name="description" content="" /> <META http-equiv=Content-Type content="text/html; charset=utf-8"> <style type="text/css"> ul {height:200px; padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px } #announcement { width:300px; height:200px; background:url(img/menu_bg.gif) repeat; overflow: hidden; } #announcement div { border: #e6e6e6 1px solid; padding:0px 10px 0px 10px; overflow-y:hidden; line-height: 24px; height:100px; } #announcement li { font-size: 12px; float: left; list-style-type: none; margin-right: 20px; padding-left: 10px; background: url(img/arrow_right.gif) no-repeat 0px 50%; white-space: nowrap } #announcement a { text-decoration: none; } #announcement a:hover { text-decoration:underline; } </style> </HEAD> <BODY> <DIV id="announcement" onMouseOver="if(!anncount) {clearTimeout(annst);annst = 0}" onMouseOut="if(!annst) annst = setTimeout('announcementScroll()', anndelay);"> <DIV id="scrbody"> <ul> <li> <a href="#" target="_blank">jQuery 類似騰訊網的圖片幻燈特效(可自動播放)</a> </li> <li> <a href="#/JS" target="_blank">VB版增強型Windows任務管理器</a> </li> <li> <a href="#/JS/texiao"target="_blank">JQuery Tip多風格鏈接提示框</a> </li> <li> <a href="#/JS/ad" target="_blank">VC++動態加載、調用smtp.dll發郵件示例</a> </li> <li> <a href="#/html+css" target="_blank">++連連看游戲源碼附外掛</a> </li> <li> <a href="#/" target="_blank">基於API的VB HOOK鉤子攔截程序</a> </li> <li> <a href="#/" target="_blank">VB 操作系統的一些常用小技巧集</a> </li> <li> <a href="#/js" target="_blank">xTree 標准的WEB菜單樹(樹形菜單)</a> </li> </ul> </DIV> </DIV> <script type="text/javascript"> function $(id) { return document.getElementById(id); } var anndelay = 3000; var anncount = 0; var annheight = 24; var annst = 0; function announcementScroll() { if( ! annst) { $('scrbody').innerHTML += '<br style="clear: both" />' + $('scrbody').innerHTML; $('scrbody').scrollTop = 0; if($('scrbody').scrollHeight > annheight * 3) { annst = setTimeout('announcementScroll()', anndelay); } else { $('announcement').onmouseover = $('announcement').onmouseout = null; } return; } if(anncount == annheight) { if($('scrbody').scrollHeight - annheight <= $('scrbody').scrollTop) { $('scrbody').scrollTop = $('scrbody').scrollHeight / 2 - annheight; } anncount = 0; annst = setTimeout('announcementScroll()', anndelay); } else { $('scrbody').scrollTop ++ ; anncount ++ ; annst = setTimeout('announcementScroll()', 10); } } announcementScroll(); </script> </BODY> </HTML>
關鍵是這一行:
$('scrbody').innerHTML += '<br style="clear: both" />' + $('scrbody').innerHTML;
它把內容複製一份接在後面,讓總高度變成原本的兩倍。接著當 scrollTop 滾到「總高度的一半」時,就把它重設回去——因為前後兩段的視覺內容完全相同,這個跳躍肉眼看不到,於是形成無限循環的錯覺。
那個 <br style="clear: both" /> 是必要的:li 用了 float 浮動,若沒有清除浮動,innerHTML 的兩份內容可能會並排而非上下堆疊,複製就失去意義。
程式碼用兩個計時器變速來製造節奏:anncount 累計已滾動的像素,每 10 毫秒 scrollTop++ 一次;當累積達到 annheight(24px,剛好等於 line-height)時,改成停 3 秒(anndelay)再繼續。這就形成「滾一行 → 停頓 → 滾下一行」的效果。
滑鼠移入時清空計時器並把 annst 設為 0,移出時重新排程——這是讓使用者能看清內容的基本設計。
innerHTML += 會重建整棵 DOM。每次這樣寫,瀏覽器都會把現有節點丟掉重新解析。公告內容多時,這一行的代價不小,且原本掛在子元素上的事件都會消失。改用 cloneNode(true) 複製節點再 append,或用 insertAdjacentHTML,都能避開重建。
setTimeout 傳字串等於 eval。setTimeout('announcementScroll()', anndelay) 會走字串解析路徑,比傳函數參照慢,且在嚴格模式或 CSP 政策下可能直接失效。應寫成 setTimeout(announcementScroll, anndelay)。
annst 同時當旗標與計時器 ID 用。移入時設為 0 表示「已暫停」,但 setTimeout 也可能回傳 0(實務上瀏覽器回傳的都是正整數,所以僥倖能動)。這種重