DIV CSS 佈局教程網

無縫滾動進階版:支援上下左右四向,並封裝成函式
編輯:關於JavaScript     

無縫滾動最關鍵的技巧只有一句:把內容複製一份接在自己後面。這樣當第一份滾出視線,第二份正好補上,視覺上永遠看不到斷點;再在位移超過一半時歸零,就能無限循環。

頁面結構

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>無縫滾動——上下</title>
    <style type="text/css">
    *{margin:0;padding:0;}
    li{list-style:none;}
    img{border:0;}
    #scroll{width:178px;margin:50px auto;position:relative;}
    .btn{display:block;width:27px;height:27px;position:absolute;left:75px;}
    .up{background:url(images/up.gif);top:0;}
    .down{background:url(images/down.gif);top:490px;}
    .content{height:440px;overflow:hidden;position:relative;top:40px;}
    .content ul{position:absolute;top:0;left:0;}
    .content li{height:110px;}
    </style>
</head>
<body>
    <div id="scroll">
        <a href="javascript:;" class="btn up"></a>
        <a href="javascript:;" class="btn down"></a>
        <div class="content">
             <ul>
                <li><a href="#" title="111"><img src="images/1.jpg" alt="111" width="178" height="108"/></a></li>
                <li><a href="#" title="222"><img src="images/2.jpg" alt="222" width="178" height="108"/></a></li>
                <li><a href="#" title="333"><img src="images/3.jpg" alt="333" width="178" height="108"/></a></li>
                <li><a href="#" title="444"><img src="images/4.jpg" alt="444" width="178" height="108"/></a></li>
             </ul>
        </div>
    </div>
</body>
</html>
<script type="text/javascript" src="scroll.js"></script>
<script type="text/javascript">
window.onload = function(){
    scroll('top', 1, 1000);
};
</script>

scroll.js

/**********
   功能:實現水平或垂直無縫滾動
   參數:direction 方向,總共 4 個值:left, right, top, bottom
         speed 移動距離
         iTime 多少時間後開始移動,若不寫則頁面加載完開始移動
**********/
function scroll(direction, speed, iTime){
   var oDiv = document.getElementById('scroll');
   var oUl  = oDiv.getElementsByTagName('ul')[0];
   var aLi  = oDiv.getElementsByTagName('li');
   var aBtn = oDiv.getElementsByTagName('a');
   var timer = null;
   var iSpeed = 0;
   var flag = true;      // 判斷水平移動還是垂直移動

   oUl.innerHTML += oUl.innerHTML;   // 複製一份,無縫的關鍵

   switch(direction){
     case 'left':
       iSpeed = -speed;
       oUl.style.width = aLi[0].offsetWidth * aLi.length + 'px';
       flag = true;
       break;
     case 'right':
       iSpeed = speed;
       oUl.style.width = aLi[0].offsetWidth * aLi.length + 'px';
       flag = true;
       break;
     case 'top':
       iSpeed = -speed;
       flag = false;
       break;
     case 'bottom':
       iSpeed = speed;
       flag = false;
       break;
   };

   setTimeout(move, iTime);

   // 左按鈕和上按鈕
   aBtn[0].onclick = function(){
     clearInterval(timer);
     iSpeed = -speed;
     move();
   };

   // 右按鈕和下按鈕
   aBtn[1].onclick = function(){
     clearInterval(timer);
     iSpeed = speed;
     move();
   };

   oUl.onmouseover = function(){
     clearInterval(timer);   // 滑入暫停
   };

   oUl.onmouseout = function(){
     move();                 // 滑出繼續
   };

   function move(){
     timer = setInterval(function(){
       if(flag){
         oUl.style.left = oUl.offsetLeft + iSpeed + 'px';
         if(oUl.offsetLeft < -oUl.offsetWidth / 2){
           oUl.style.left = '0';
         }else if(oUl.offsetLeft > 0){
           oUl.style.left = -oUl.offsetWidth / 2 + 'px';
         }
       }else{
         oUl.style.top = oUl.offsetTop + iSpeed + 'px';
         if(oUl.offsetTop <= -oUl.offsetHeight / 2){
           oUl.style.top = '0';
         }else if(oUl.offsetTop >= 0){
           oUl.style.top = -oUl.offsetHeight / 2 + 'px';
        };
       };
     }, 30);
   };
}

幾個重點:flag 決定走水平(改 left、要設 width)還是垂直(改 top);歸零判斷一律用「元素總寬高的一半」,因為內容已經複製成兩份。onmouseover 清計時器、onmouseout 重新啟動,是讓使用者能停下來點連結的必要設計。

使用上有個硬性前提:HTML 結構必須與上面完全一致——ul 要是 content 的直接子元素且定位為 absolute,li 尺寸要固定,因為程式是靠 offsetWidth/offsetHeight 計算的。若要更進一步,可以把 innerHTML += 換成 cloneNode(true)(前者會重建節點、遺失已綁定的事件),並改用 requestAnimationFrame 取代 setInterval,滾動會順得多。

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