DIV CSS 佈局教程網

JavaScript 左右無縫滾動實作:多圖同時流動的首頁輪播
編輯:關於JavaScript     

之前談過圖片的間歇式左右滾動,也就是一張一張地切換。這次要介紹的是另一種常見做法——讓好幾張圖片一起連續地無縫流動,非常適合用於首頁的商品或廣告輪播。

 <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"> <title>無縫滾動——左右</title> <link rel="stylesheet" type="text/css" href="../css/base.css" media="all"/> <style type="text/css"> #scroll{width:698px;height:108px;margin:50px auto 0;position:relative;overflow:hidden;} .btn_left{display:block;width:68px;height:68px;background:url(images/btn.jpg) no-repeat -70px -69px;position:absolute;top:20px;left:1px;z-index:1;} .btn_left:hover{background:url(images/btn.jpg) no-repeat -70px 0;} .btn_right{display:block;width:68px;height:68px;background:url(images/btn.jpg) no-repeat 1px -69px;position:absolute;top:20px;right:0;z-index:1;} .btn_right:hover{background:url(images/btn.jpg) no-repeat 1px 0;} #scroll .content{width:546px;height:108px;position:relative;overflow:hidden;margin:0 auto;} #scroll ul{position:absolute;} #scroll li{float:left;width:182px;height:108px;text-align:center;} #scroll li a:hover{position:relative;top:2px;} </style> </head> <body> <div id="scroll"> <a href="javascript:;" class="btn_left"></a> <a href="javascript:;" class="btn_right"></a> <div class="content"> <ul> <li><a href="#"><img src="images/1.jpg" width="178" height="108" alt=""/></a></li> <li><a href="#"><img src="images/2.jpg" width="178" height="108" alt=""/></a></li> <li><a href="#"><img src="images/3.jpg" width="178" height="108" alt=""/></a></li> <li><a href="#"><img src="images/4.jpg" width="178" height="108" alt=""/></a></li> </ul> </div> </div> </body> </html> <script type="text/javascript"> window.onload = function(){ var oDiv = document.getElementById('scroll'); var oUl = oDiv.getElementsByTagName('ul')[0]; var aLi = oDiv.getElementsByTagName('li'); var aBtn = oDiv.getElementsByTagName('a'); var speed = -1; var timer = null; oUl.innerHTML += oUl.innerHTML; oUl.style.width = aLi[0].offsetWidth * aLi.length + 'px'; timer = setInterval(function(){ oUl.style.left = oUl.offsetLeft + speed + 'px'; if(oUl.offsetLeft < - oUl.offsetWidth / 2){ oUl.style.left = '0'; }else if(oUl.offsetLeft > 0){ oUl.style.left = - oUl.offsetWidth / 2 + 'px'; } },30); aBtn[0].onclick = function(){ speed = -1; }; aBtn[1].onclick = function(){ speed = 1; }; oUl.onmouseover = function(){ clearInterval(timer); }; oUl.onmouseout = function(){ timer = setInterval(function(){ oUl.style.left = oUl.offsetLeft + speed + 'px'; if(oUl.offsetLeft < -oUl.offsetWidth / 2){ oUl.style.left = '0'; }else if(oUl.offsetLeft > 0){ oUl.style.left = - oUl.offsetWidth / 2 + 'px'; } },30); }; }; </script> 

實作上的關鍵有幾個:先複製一份列表內容讓總寬度加倍,再用 aLi[0].offsetWidth * aLi.length 精確算出 ul 的寬度;移動時透過 offsetLeft 判斷是否越過一半,若是就重置位置,从而形成首尾相接的無縫效果。左右兩個按鈕分別把 speed 設為 -1 與 1,滑鼠移入暫停、移出續跑,互動體驗更完整。

PS:想調整速度,只要改 speed 的值即可。

更多 JavaScript 相關內容,有興趣的讀者可延伸閱讀切換特效、查找演算法、動畫特效、錯誤除錯、資料結構、遍歷演算法與數學運算等系列主題。

希望這份左右無縫滾動的實作能對你的 JavaScript 開發有所幫助。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved