DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
打字機效果入門:DisplayMsg 如何控制逐字顯示的節奏
編輯:CSS詳解     

讓文字一個字一個字跳出來,模擬終端機或打字機的感覺。這個範例不只做逐字顯示,還加入了三種不同的停頓長度,讓節奏聽起來更自然。

程式碼

<HEAD>
<SCRIPT LANGUAGE="JavaScript">
var outMsg = "";
var i = 0;
var lineNo = 1;
var timerDM = null;

function araVob() { }
var ScreenLine = new araVob();

ScreenLine[1] = "welcome to knowsky.com"
ScreenLine[2] = "All the free technology you can handle"
ScreenLine[3] = "You Can also visit"
ScreenLine[4] = "https://www.knowsky.com"
ScreenLine[5] = "Please keep submitting"
ScreenLine[6] = "Let us know what you think"
ScreenLine[7] = "Thanks Alot"
ScreenLine[8] = " Happy !!"

var msgNum  = 1;
var msgCnt  = 8;
var typeSpeed = 200;   // 每個字的間隔(毫秒)
var pageLen = 4;       // 每頁顯示幾行
var delay = typeSpeed;
var r = 0;
var cr = "\r\n"
if ("3" <= navigator.appVersion.charAt(0)) { var cr = "\n" }

for (x = 1; x <= msgCnt; x++) { ScreenLine[x] = ScreenLine[x] + cr; }
msg = ScreenLine[1];

function DisplayMsg() {
  if (msg.length <= i || msg.charAt(i) == "\f") {
    r = i;
    i = 0;
    ChangeMsg();
  }
  outMsg = outMsg + msg.charAt(i);
  i++;

  // 三種不同的停頓
  if (msg.charAt(i) == "\f" || (lineNo == pageLen && i == msg.length)) {
    delay = 4000;                       // 換頁停 4 秒
  } else {
    if (msg.charAt(i) == cr && msg != " " + cr) {
      delay = 2000;                     // 換行停 2 秒
    } else {
      delay = typeSpeed;                // 一般字停 0.2 秒
    }
  }

  self.document.forms[0].elements[0].value = outMsg;
  timerDM = setTimeout("DisplayMsg()", delay);
}

function ChangeMsg() {
  msgNum++;
  if (msgCnt < msgNum) { msgNum = 1; }
  lineNo++;
  if (pageLen < lineNo || msg.charAt(r) == "\f") {
    outMsg = ScreenLine[msgNum].charAt(i);
    i++;
    lineNo = 1;
  }
  msg = ScreenLine[msgNum];
}
// End -->
</script>
</HEAD>

<BODY>
<FORM NAME="msgform" ACTION="">
  <input type="button" value="點擊這裡查看" onClick="DisplayMsg()">
</FORM>
</body>

節奏是怎麼做出來的

整段程式最用心的地方在 delay 的三段判斷:一般字元停 200 毫秒,遇到換行停 2 秒,換頁停 4 秒。如果全部用固定間隔,看起來會像機器在吐字;有了長短交錯,才有「人在打字、打完抬頭想一下」的感覺。這個「非等速節奏」的觀念,做進度提示、聊天室對話氣泡、語音字幕時都適用。

charAt(i) 每次取一個字元接在 outMsg 後面,再寫回表單欄位,這就是逐字顯示的核心。\f 被當成分頁符號使用,pageLen = 4 則限制每頁最多 4 行。

原始碼裡那個 function araVob() { } 是個空函式,被拿來當陣列用——在正式陣列語法普及前的權宜之計。今天直接用 const ScreenLine = [...] 就好。

現代重寫

const lines = [
  "歡迎來到這個網站",
  "所有好用的技術都在這裡",
  "也歡迎你逛逛其他專區",
  "https://example.com",
  "歡迎隨時投稿分享",
  "告訴我們你的想法",
  "非常感謝",
  " 祝你有美好的一天 !!"
];

const output = document.getElementById("output");

function type(lines, { charDelay = 60, lineDelay = 600 } = {}) {
  let li = 0, ci = 0, buf = "";

  function step() {
    if (li >= lines.length) return;

    const line = lines[li];
    if (ci < line.length) {
      buf += line[ci++];
      output.textContent = buf;
      setTimeout(step, charDelay);
    } else {
      buf += "\n";
      li++; ci = 0;
      output.textContent = buf;
      setTimeout(step, lineDelay);
    }
  }
  step();
}

document.getElementById("start").addEventListener("click", () => type(lines));

幾個實作上的差異值得注意:用陣列字面值取代自製的容器物件;用 textContent 而非 value,輸出到一般元素而非表單欄位(表單欄位會干擾使用者輸入,也不利於複製整段文字);參數物件讓間隔可從外部調整。

還有兩個體貼的補充。第一,長時間跑 setTimeout 在背景分頁會被降頻,回來時動畫可能卡住,記得在 visibilitychange 時處理。第二,也是更重要的:螢幕閱讀器不適合逐字播報,每個字都觸發一次朗讀會完全無法理解。替輸出元素加上 aria-live="off",或另外提供一份完整的靜態文字給輔助技術使用,才是負責任的做法。同時也別忘了 prefers-reduced-motion——對部分使用者而言,逐字跳動的文字確實會造成不適。

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