萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

純 HTML 實現文字滾動自動停止: 標籤完整範例

效果預覽

文字從右向左滑入,完整顯示後自動停止,不重複播放。

完整代碼範例

<!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>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  <title>文字滾動自動停止範例</title>
</head>
<body>
  <p align="left">
    <marquee height="293" behavior="slide" width="550" loop="1"
             scrollamount="1" scrolldelay="100">
      <font style="font-size: 18pt; font-family: 方正姚體; color: #ea0000">
        <strong>代碼滾動</strong>
      </font>
    </marquee>
  </p>
</body>
</html>

關鍵屬性解析

屬性說明
behavior="slide"滑入後停止(而非持續捲動 scroll 或來回彈跳 alternate)
loop="1"僅播放一次
scrollamount="1"每步移動像素(越小越平滑)
scrolldelay="100"每步延遲毫秒(越大越慢)

現代替代方案

<marquee> 為非標準專屬標籤(IE 起源),現代專案建議使用 CSS Animation 或 JavaScript 實現:

@keyframes slide-in {
  from { transform: translateX(100%); }
  to   { transform: translateX(0); }
}
.marquee-text {
  animation: slide-in 5s ease-out forwards;
  white-space: nowrap;
  overflow: hidden;
}

優點:符合標準、可控性強、支援暫停/續播/無障礙屬性。

copyright © 萬盛學電腦網 all rights reserved