這段效果程式碼寫得相當完整:支援上下左右四個方向的無縫循環滾動,滑鼠移入暫停、移出繼續,還能自動讀取指定資料夾下的圖片來播放。值得收藏起來當範本。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="https://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/> <title>滾動測試</title> <script type="text/javascript"> /** * @para obj 目標對象 如:demo,deml1,demo2 中的"demo" 可任意,只要不重復 * * @para speed 滾動速度 越大越慢 * * @para direction 滾動方向 包括:left,right,down,up * * @para objWidth 總可見區域寬度 * * @para objHeight 總可見區域高度 * * @para filePath 存放滾動圖片的路徑 (如果是自動獲取文件夾裡的圖片滾動) * * @para contentById 對某id為contentById下的內容進行滾動 此滾動與filePath不能共存請注意 * * @para 用法實例 scrollObject("res",50,"up",470,200,"","resource") 對contentById(resource)下內容進行滾動 * * @para 用法實例 scrollObject("res",50,"up",470,200,"d:\\images\\","") 對filePath(images)下內容自動獲取並進行滾動,目前只支持ie */ var $ =function(id){return document.getElementById(id)} // 滾動 function scrollObject(obj,speed,direction,objWidth,objHeight,filePath,contentById) { // 執行初始化 if(direction=="up"||direction=="down") document.write(UDStructure()); else document.write(LRStructure()); var demo = $(obj); var demo1 = $(obj+"1"); var demo2 = $(obj+"2"); var speed=speed; $(contentById).style.display="none" demo.style.overflow="hidden"; demo.style.width = objWidth+"px"; demo.style.height = objHeight+"px"; demo.style.margin ="0 auto"; if(filePath!="") demo1.innerHTML=file(); if(contentById!="") demo1.innerHTML=$(contentById).innerHTML; demo2.innerHTML=demo1.innerHTML; // 左右滾動 function LRStructure() { var _html =""; _html+="<div id='"+obj+"' >"; _html+="<table border='0' align='left' cellpadding='0' cellspacing='0' cellspace='0'>"; _html+="<tr>"; _html+="<td nowrap='nowrap' id='"+obj+"1' >"; // 此處是放要滾動的內容 _html+="</td>"; _html+="<td nowrap='nowrap' id='"+obj+"2' ></td>"; _html+="</tr>"; _html+="</table>"; _html+="</div>"; return _html; } // 上下滾動的結構 function UDStructure() { var _html =""; _html+="<div id="+obj+" >"; _html+="<table border='0' align='left' cellpadding='0' cellspacing='0' cellspace='0'>"; _html+="<tr>"; _html+="<td id='"+obj+"1' >"; // 此處是放要滾動的內容 _html+="</td>"; _html+="</tr>"; _html+="<tr>"; _html+="<td id='"+obj+"2' ></td>"; _html+="</tr>"; _html+="</table>"; _html+="</div>"; return _html; } // 取得文件夾下的圖片 function file() { var tbsource = filePath;//本地文件夾路徑 filePath = filePath.toString(); if (filePath=="") return""; var imgList =""; var objFSO =new ActiveXObject('Scripting.FileSystemObject'); // 文件夾是否存在 if(!objFSO.FolderExists(tbsource)) { alert("<"+tbsource+">該文件夾路徑不存在,或者路徑不能含文件名!"); objFSO =null; return; } var objFolder = objFSO.GetFolder(tbsource); var colFiles =new Enumerator(objFolder.Files); var re_inf1 =/\.jpg$/; //驗證文件夾文件是否jpg文件 for (;!colFiles.atEnd();colFiles.moveNext()) //讀取文件夾下文件 { var objFile = colFiles.item(); if(re_inf1.test(objFile.Name.toLowerCase())) { imgList +="<img src="+filePath+"/"+objFile.Name+">"; } } return imgList; } // 向左滾 function left() { if(demo2.offsetWidth-demo.scrollLeft<=0) { demo.scrollLeft-=demo1.offsetWidth; } else { demo.scrollLeft++; } } // 向右滾 function right() { if(demo.scrollLeft<=0) { demo.scrollLeft+=demo2.offsetWidth; } else { demo.scrollLeft-- } } // 向下滾 function down() { if(demo1.offsetTop-demo.scrollTop>=0) { demo.scrollTop+=demo2.offsetHeight; } else { demo.scrollTop-- } } // 向上滾 function up() { if(demo2.offsetTop-demo.scrollTop<=0) { demo.scrollTop-=demo1.offsetHeight; } else { demo.scrollTop++ } } // 切換方向 function swichDirection() { switch(direction) { case"left": return left(); break; case"right": return right(); break; case"up": return up(); break; default: return down(); } } // 重復執行 var myMarquee=setInterval(swichDirection,speed); // 鼠標懸停 demo.onmouseover=function() {clearInterval(myMarquee);} // 重新開始滾動 demo.onmouseout=function() { myMarquee=setInterval(swichDirection,speed);} } </script> </head> <body> <div id="img"> <table width="1000" border="0" align="center" cellpadding="5" cellspacing="0"> <tr> <td width="200"><img src="https://attach.e.iciba.com/attachment/200910/22/4188617_12561994098532.jpg" alt="" width="200" height="150"/></td> <td width="200"><img src="https://pica.nipic.com/2008-05-27/2008527145211519_2.jpg" alt="" width="200" height="150"/></td> <td width="200"><img src="https://pic4.nipic.com/20090823/383152_215728074589_2.jpg" alt="" width="200" height="150"/></td> <td width="200"><img src="https://pic8.nipic.com/20100628/4643449_170245009531_2.jpg" alt="" width="200" height="150"/></td> <td width="200"><img src="https://pica.nipic.com/2008-05-30/20085309524648_2.jpg" alt="" width="200" height="150"/></td> </tr> </table> </div> <script type="text/javascript"> scrollObject("sr",50,"right",800,160,"","img") </script> </body> </html>
關鍵在於複製一份內容。程式把要滾動的內容同時放進 demo1 和 demo2 兩個容器,然後:
當 demo1 完全滾出視野的瞬間,把 scrollTop 減去 demo1 的高度,視覺上看起來就像回到起點,但因為內容是連續重複的,使用者察覺不到跳躍。左右方向同理,只是改用 scrollLeft 與 offsetWidth。
這個「複製一份 + 到臨界點歸零」的技巧,是所有無縫循環滾動的共同基礎,理解之後自己改寫也不難。
參數設計也值得學:speed 控制間隔(數值越大越慢),direction 決定方向,objWidth/objHeight 決定可視區域大小,contentById 指定要滾動的既有內容。滑鼠移入用 clearInterval 暫停、移出重新 setInterval 繼續,這兩個事件搭配是跑馬燈的標準做法。
有幾點必須指出,否則搬進新專案會出問題。
第一,file() 裡的 ActiveXObject 只在 IE 下有效,而且早已被微軟淘汰。瀏覽器基於安全不允許網頁讀取本機任意資料夾路徑,這段功能在現代環境完全不能用。要動態載入圖片,正確做法是把圖片清單當資料由後端提供,或用 <input type="file" multiple> 讓使用者主動挑選。
第二,用 document.write 動態產生 DOM 是很危險的做法。它會在頁面載入完成後清空整個文件。應該改用 document.createElement 或 innerHTML 指派。
第三,setInterval 每次位移 1px,在現代瀏覽器上會看起來不太順。用 requestAnimationFrame 會平順得多,也能自動配合螢幕更新率。
第四,那幾個外連圖片的網址多半早已失效。範例中的圖片託管服務年代久遠,請替換成自己的素材。
第五,無限自動滾動的內容對部分使用者(尤其是有前庭敏感或注意力障礙的人)會造成困擾。除了滑鼠移入暫停,建議也尊重系統的 prefers-reduced-motion 設定,並提供明確的暫停控制項,這既是無障礙要求,也是使用體驗的一部分。
把這些觀念替換進去,這份範本的骨架依然很好用。