頁面的自動滾動效果通常以 JavaScript 實作,但其實還有一個更省事的做法——HTML 的 <marquee></marquee> 標籤,就能做出多種滾動效果,完全不需要 JS 控制。使用 marquee 不僅能滾動文字,也能滾動圖片、表格等元素。
語法:<marquee>...</marquee>,在標籤之間放入要滾動的內容即可。
1. direction(滾動方向):可為 up、down、left、right。
<marquee direction="滾動方向">...</marquee>
2. behavior(滾動方式):scroll 為循環滾動(預設)、slide 只滾動一次即停止、alternate 來回交替滾動。
<marquee behavior="滾動方式">...</marquee>
3. scrollamount(滾動速度):設定每次移動的長度,單位為像素。
<marquee scrollamount="5">...</marquee>
4. scrolldelay(滾動延遲):設定滾動的時間間隔,單位為毫秒。
<marquee scrolldelay="100">...</marquee>
5. loop(滾動循環):預設值為 -1,代表不斷循環。
<marquee loop="2">...</marquee>
6. width、height(滾動範圍);7. bgcolor(背景顏色);8. hspace、vspace(水平/垂直留白)。
<marquee direction="up" behavior="scroll" scrollamount="1" scrolldelay="0"
loop="-1" width="1000" height="50" bgcolor="#0099FF" hspace="10" vspace="10">
這裡放入要滾動的內容
</marquee>
其中 behavior 的 scroll 表示由一端滾到另一端並重複(缺點是無法無縫滾動),slide 則是滾動一次後停止、不重複。