本文實例講述了JS簡單實現無縫滾動效果。分享給大家供大家參考,具體如下:
<!doctype html> <title>javascript無縫滾動</title> <meta charset="utf-8" /> <meta name="keywords" content="javascript無縫滾動" /> <meta name="description" content="javascript無縫滾動" /> <style type="text/css"> h1 { font: 400 16px/1 "Microsoft YaHei", KaiTi_GB2312, SimSun } #marquee { position: relative; width: 400px; overflow: hidden; border: 10px solid #8080C0; } #marquee img { border: 0px; } #marquee dl, #marquee dt, #marquee dd, #marquee a { float: left; margin: 0; padding: 0; } #marquee dl { width: 1000%; height: 150px; } </style> <script type="text/javascript"> var Marquee = function(id) { try { document.execCommand("BackgroundImageCache", false, true); } catch(e) {}; var container = document.getElementById(id), original = container.getElementsByTagName("dt")[0], clone = container.getElementsByTagName("dd")[0], speed = arguments[1] || 10; clone.innerHTML = original.innerHTML; container.scrollLeft = clone.offsetLeft var rolling = function() { if(container.scrollLeft == 0) { container.scrollLeft = clone.offsetLeft; } else { container.scrollLeft--; } } var timer = setInterval(rolling, speed) //設置定時器 container.onmouseover = function() { clearInterval(timer) } //鼠標移到marquee上時,清除定時器,停止滾動 container.onmouseout = function() { timer = setInterval(rolling, speed) } //鼠標移開時重設定時器 } window.onload = function() { Marquee("marquee"); } </script> <h1>javascript無縫滾動(向右滾動)</h1> <div id="marquee"> <dl> <dt> <a href="###"><img src="img/o_s017.jpg" alt="javascript無縫滾動"/></a> <a href="###"><img src="img/o_s018.jpg" alt="javascript無縫滾動"/></a> <a href="###"><img src="img/o_s019.jpg" alt="javascript無縫滾動"/></a> <a href="###"><img src="img/o_s020.jpg" alt="javascript無縫滾動"/></a> <a href="###"><img src="img/o_s021.jpg" alt="javascript無縫滾動"/></a> <a href="###"><img src="img/o_s022.jpg" alt="javascript無縫滾動"/></a> <a href="###"><img src="img/o_s023.jpg" alt="javascript無縫滾動"/></a> </dt> <dd></dd> </dl> </div>
效果圖如下:
更多關於JavaScript相關內容感興趣的讀者可查看本站專題:《JavaScript切換特效與技巧總結》、《JavaScript查找算法技巧總結》、《JavaScript動畫特效與技巧匯總》、《JavaScript錯誤與調試技巧總結》、《JavaScript數據結構與算法技巧總結》、《JavaScript遍歷算法與技巧總結》及《JavaScript數學運算用法總結》
希望本文所述對大家JavaScript程序設計有所幫助。