DIV CSS 佈局教程網

JS 逐行文字循環滾動:帶停頓效果完整實作教學
編輯:關於JavaScript     

首頁公告、通知列表、門診叫號、簡報式廣告位……只要需要在有限高度裡輪播多行文字,「一行一行往上捲、換行時停一下」幾乎是標配。純 CSS 動畫很難精準控制停留節奏,改用 JavaScript 反而輕鬆:把滾動邏輯封裝成一個建構函式,剩下的事情交給參數就好。

下面這份範例把整段行為包成 Marquee,支援上下左右四個方向、可調步長、可調速度,還能設定每捲動一行的停頓時間與頁面載入後的等待時間。

完整程式碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>JS 帶停頓的逐行文字循環滾動</title>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
<style type="text/css">
#scrollBox2{font-size:12px;width:260px;color:#646464;line-height:22px;height:66px;overflow:hidden;text-align:left;}
</style>
</head>
<body>
<div id="scrollBox2" style="text-align:left;">
歡迎光臨,這裡是實用的逐行文字滾動效果<br>
換行之間會有停頓,閱讀起來更輕鬆<br>
程式碼簡潔,需要的朋友可以直接取用<br>
</div>
<script>
window.onload = function(){
  new Marquee(
    "scrollBox2", // 容器 ID
    0,            // 滾動方向:0 上、1 下、2 左、3 右
    2,            // 每次移動的步長
    200,          // 容器可視寬度
    64,           // 容器可視高度
    50,           // 定時器間隔,數值越小越快(1000 = 1 秒,建議不低於 20)
    2000,         // 每輪結束後的停頓時間(0 為不停頓,1000 = 1 秒)
    1000,         // 啟動前的等待時間(0 為不等待)
    // 第八個參數為間隔滾動間距,可省略
  );
};

function Marquee(){
  this.ID       = document.getElementById(arguments[0]);
  this.Direction= arguments[1];
  this.Step     = arguments[2];
  this.Width    = arguments[3];
  this.Height   = arguments[4];
  this.Timer    = arguments[5];
  this.WaitTime = arguments[6];
  this.StopTime = arguments[7];
  if(arguments[8]){ this.ScrollStep = arguments[8]; }
  else{ this.ScrollStep = this.Direction > 1 ? this.Width : this.Height; }

  this.CTL = this.StartID = this.Stop = this.MouseOver = 0;
  this.ID.style.overflowX = this.ID.style.overflowY = "hidden";
  this.ID.noWrap = true;
  this.ID.style.width  = this.Width;
  this.ID.style.height = this.Height;
  this.ClientScroll = this.Direction > 1 ? this.ID.scrollWidth : this.ID.scrollHeight;
  this.ID.innerHTML += this.ID.innerHTML;   // 複製一份內容,才能無縫循環
  this.Start(this, this.Timer, this.WaitTime, this.StopTime);
}

Marquee.prototype.Start = function(msobj, timer, waittime, stoptime){
  msobj.StartID = function(){ msobj.Scroll(); };
  msobj.Continue = function(){
    if(msobj.MouseOver == 1){ setTimeout(msobj.Continue, waittime); }
    else{
      clearInterval(msobj.TimerID);
      msobj.CTL = msobj.Stop = 0;
      msobj.TimerID = setInterval(msobj.StartID, timer);
    }
  };
  msobj.Pause = function(){
    msobj.Stop = 1;
    clearInterval(msobj.TimerID);
    setTimeout(msobj.Continue, waittime);
  };
  msobj.Begin = function(){
    msobj.TimerID = setInterval(msobj.StartID, timer);
    msobj.ID.onmouseover = function(){
      msobj.MouseOver = 1;
      clearInterval(msobj.TimerID);
    };
    msobj.ID.onmouseout = function(){
      msobj.MouseOver = 0;
      if(msobj.Stop == 0){
        clearInterval(msobj.TimerID);
        msobj.TimerID = setInterval(msobj.StartID, timer);
      }
    };
  };
  setTimeout(msobj.Begin, stoptime);
};

Marquee.prototype.Scroll = function(){
  switch(this.Direction){
    case 0:
      this.CTL += this.Step;
      if(this.CTL >= this.ScrollStep && this.WaitTime > 0){
        this.ID.scrollTop += this.ScrollStep + this.Step - this.CTL;
        this.Pause(); return;
      } else {
        if(this.ID.scrollTop >= this.ClientScroll) this.ID.scrollTop -= this.ClientScroll;
        this.ID.scrollTop += this.Step;
      }
      break;
    case 1:
      this.CTL += this.Step;
      if(this.CTL >= this.ScrollStep && this.WaitTime > 0){
        this.ID.scrollTop -= this.ScrollStep + this.Step - this.CTL;
        this.Pause(); return;
      } else {
        if(this.ID.scrollTop <= 0) this.ID.scrollTop += this.ClientScroll;
        this.ID.scrollTop -= this.Step;
      }
      break;
    case 2:
      this.CTL += this.Step;
      if(this.CTL >= this.ScrollStep && this.WaitTime > 0){
        this.ID.scrollLeft += this.ScrollStep + this.Step - this.CTL;
        this.Pause(); return;
      } else {
        if(this.ID.scrollLeft >= this.ClientScroll) this.ID.scrollLeft -= this.ClientScroll;
        this.ID.scrollLeft += this.Step;
      }
      break;
    case 3:
      this.CTL += this.Step;
      if(this.CTL >= this.ScrollStep && this.WaitTime > 0){
        this.ID.scrollLeft -= this.ScrollStep + this.Step - this.CTL;
        this.Pause(); return;
      } else {
        if(this.ID.scrollLeft <= 0) this.ID.scrollLeft += this.ClientScroll;
        this.ID.scrollLeft -= this.Step;
      }
      break;
  }
}
</script>
</body>
</html>

幾個實作關鍵點:

初始化時那句 this.ID.innerHTML += this.ID.innerHTML 是整個效果能「無縫循環」的原因——把內容複製一份接在後面,滾到尾端時直接跳回頭,視覺上完全看不出接縫。

ScrollStep 代表「一次要滾過的可視距離」。如果沒有傳第八個參數,程式會依方向自動帶入寬度或高度;也就是說,預設行為剛好是一次捲動一整屏。

停頓是靠 CTL 這個累加器達到的:每跑一格就累加步長,累計值一旦跨過 ScrollStep,就把位置補齊到整數倍,然後呼叫 Pause() 清掉定時器,等 WaitTime 毫秒後再重新啟動。這也是為什麼停頓時間設為 0 時反而完全不會停——判斷條件裡有 WaitTime > 0。

滑鼠移入會暫停、移出接著跑,這段互動由 onmouseover / onmouseout 搭配 MouseOver 旗標完成,避免使用者正在閱讀時文字卻一直跑掉。

以上就是這份帶停頓逐行滾動效果的全部內容,需要的朋友可以替換掉容器內的文字直接套用,也歡迎依自己的版面調整寬高與節奏。

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