萬盛學電腦網

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

HTML Marquee 跑馬燈完整屬性指南與 Dreamweaver 實作技巧

前言:Marquee 標籤現狀

<marquee> 為 IE 專屬非標準標籤,HTML5 已棄用(Obsolete),但所有主流瀏覽器仍支援。現代網頁建議改用 CSS Animation 或 JavaScript 實現跑馬燈,以符合標準、支援無障礙(可暫停)、效能更佳。本文完整整理 Marquee 所有屬性,供維護舊專案參考,並提供現代替代方案。

Marquee 核心屬性完整表

屬性說明預設值建議值/範例
scrollamount每幀移動像素(速度)61~3(平滑),值越大越快
scrolldelay幀間延遲毫秒(速度反向)85通常不設,配合 scrollamount 調整
direction滾動方向leftleft、right、up、down
behavior滾動模式scrollscroll(循環)、alternate(來回)、slide(滑入停止)
width容器寬度100%像素或百分比,如 300、100%
height容器高度自動垂直滾動必設,如 160
loop循環次數infinite數字或 infinite
bgcolor背景色透明#FFFFCC
hspace / vspace水平/垂直邊距0像素值
onmouseover="this.stop()"滑鼠進入停止—事件屬性,非標準屬性
onmouseout="this.start()"滑鼠離開繼續—事件屬性,非標準屬性

常見實作範例

基本單行跑馬燈

<marquee scrollamount="2" width="300">公告:系統將於今晚維護</marquee>

帶超連結

<marquee scrollamount="2" width="300">
  <a href="https://example.com" target="_blank">點擊查看詳細公告</a>
</marquee>

滑鼠懸停暫停

<marquee scrollamount="2" width="300" onmouseover="this.stop()" onmouseout="this.start()">
  重要通知:請詳閱條款...
</marquee>

來回滾動

<marquee scrollamount="2" width="200" behavior="alternate">
  歡迎光臨!
</marquee>

多行垂直向上滾動(公告欄常用)

<marquee scrollamount="2" width="300" height="160" direction="up">
  • 早晨好啊!<br>
  • 空氣好清新啊<br>
  • 今朝食乜好呢?<br>
  • <a href="https://example.com">腳本之家</a>
</marquee>

FrontPage 相容性陷阱與解決方案

FrontPage 儲存時會重排 Marquee 屬性順序,導致多行垂直滾動失效(僅顯示第一行)。

解決方案:嵌入 document.write

<script>
document.write('<marquee scrollamount="2" width="300" height="160" direction="up">'
  + '• 早晨好啊!<br>'
  + '• 空氣好清新啊<br>'
  + '• 今朝食乜好呢?<br>'
  + '• <a href="https://example.com">腳本之家</a>'
  + '</marquee>');
</script>

原理:繞過 FrontPage 對靜態 HTML 的重寫,由 JS 動態輸出完整標籤。

現代替代方案:CSS Animation 跑馬燈(推薦)

單行水平無限循環

<style>
.marquee-box { overflow: hidden; white-space: nowrap; width: 300px; }
.marquee-content { display: inline-block; padding-left: 100%; 
  animation: marquee 10s linear infinite; }
@keyframes marquee { to { transform: translateX(-100%); } }
.marquee-box:hover .marquee-content { animation-play-state: paused; }
</style>

<div class="marquee-box">
  <span class="marquee-content">公告:系統將於今晚維護,請提前備份資料。</span>
</div>

多行垂直無限循環

<style>
.v-marquee { height: 160px; overflow: hidden; position: relative; }
.v-marquee ul { margin: 0; padding: 0; list-style: none; 
  animation: v-marquee 8s linear infinite; }
.v-marquee li { line-height: 1.6; }
.v-marquee:hover ul { animation-play-state: paused; }
@keyframes v-marquee { to { transform: translateY(-50%); } }
/* 需將內容複製一份接在後面以實現無縫循環,JS 輔助較佳 */
</style>

<div class="v-marquee">
  <ul>
    <li>早晨好啊!</li>
    <li>空氣好清新啊</li>
    <li>今朝食乜好呢?</li>
    <li><a href="#">腳本之家</a></li>
  </ul>
</div>

無障礙與 SEO 建議

  • 必須提供暫停機制:WCAG 2.1 要求移動內容可暫停。Marquee 用 onmouseover/out,CSS 用 animation-play-state: paused。
  • 避免關鍵內容僅在跑馬燈出現:螢幕閱讀器可能無法完整讀取,重要資訊應靜態呈現。
  • SEO 影響:搜尋引擎可抓取 Marquee 內文字,但權重極低;CSS 動畫版本內容在 DOM 中完整可見,較有利。

結語

Marquee 雖簡便但屬非標準棄用技術。新專案請採用 CSS Animation + 無障礙暫停機制;維護舊專案時,掌握完整屬性表與 FrontPage 相容技巧可快速修復問題。技術債務最終仍需透過重構償還。

copyright © 萬盛學電腦網 all rights reserved