HTML如何實現滾動文字


一、HTML滾動文字(轉)

轉自:HTML滾動文字 - 蔣固金(jianggujin)的專欄 - CSDN博客
https://blog.csdn.net/jianggujin/article/details/70832469

marquee 滾動文字標簽

在一個頁面中會有很多多媒體元素,比如動態文字、動態圖象、音視頻等,而最簡單的就是天價滾動文字了,在HTML中,如果我們想要添加滾動文字,需要使用marquee標簽。 
我們先來看一下最簡單的示例:

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>marquee</title> </head> <body style="background: black;padding: 20px;"> <marquee><span style="font-weight: bolder;font-size: 40px;color: white;">Welcom CSDN!</span></marquee> </body> </html>

為了顯示效果更明顯,這里將頁面背景設置為黑色,將滾動文字設置為白色,顯示效果如圖: 
這里寫圖片描述
這樣我們就實現了一個最簡單的滾動文字,在滾動文字中還有一些屬性用於控制滾動方向、滾動速度等,下面我們就來看一下幾個比較常用的屬性。

direction 滾動方向屬性

默認情況下,文字從右向左滾動,實際應用中,我們可能需要改變方向,就可以通過該屬性來設置,該屬性可用值有:updownleftright,分別表示向上、向下、向左和向右滾動。 
示例如下:

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>marquee</title> <style> body { background: black; padding: 20px; } marquee { font-weight: bolder; font-size: 40px; color: white; } </style> </head> <body> <marquee direction="up">UP</marquee> <marquee direction="down">DOWN</marquee> <marquee direction="left">LEFT</marquee> <marquee direction="right">RIGHT</marquee> </body> </html>

這里寫圖片描述

behavior 滾動方式屬性

通過behavior 可以設置滾動方式,如往復運動等。behavior可用值有:scrollslidealternate,分別表示循環滾動、只滾動一次就停止和來回交替滾動。 
示例如下:

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>marquee</title> <style> body { background: black; padding: 20px; } marquee { font-weight: bolder; font-size: 40px; color: white; } </style> </head> <body> <marquee behavior="scroll">scroll</marquee> <marquee behavior="slide">slide</marquee> <marquee behavior="alternate">alternate</marquee> </body> </html>

這里寫圖片描述

scrolldelay 滾動延遲屬性與scrollamount 滾動速度屬性

通過scrolldelay屬性可以設置文字滾動的時間間隔。scrolldelay 的時間間隔單位是毫秒,這一時間間隔設置為滾動兩步之間的時間間隔,如果時間過長,則會出現走走停停的效果。 
scrollamount 用於設置滾動的步長。 
示例如下:

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>marquee</title> <style> body { background: black; padding: 20px; } marquee { font-weight: bolder; font-size: 40px; color: white; } </style> </head> <body> <marquee scrolldelay="800" scrollamount="100">Welcom CSDN!</marquee> </body> </html>

這里寫圖片描述

loop 滾動循環屬性

如果我們希望文字滾動幾次后停止,就可以使用loop屬性。

示例如下:

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>marquee</title> <style> body { background: black; padding: 20px; } marquee { font-weight: bolder; font-size: 40px; color: white; } </style> </head> <body> <marquee loop="2">Welcom CSDN!</marquee> </body> </html>
 
 

三、html5 css3實現字幕滾動的效果

參考:html5 css3實現字幕滾動的效果 - 5big的博客 - CSDN博客
https://blog.csdn.net/u012426959/article/details/78665858

html5中marquee逐漸被取代,要實現字幕的滾動可以再js中實現:

 

 

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>html5</title> <style> body { margin: 0px; font-size: 12px; color: #938C43; line-height: 150%; text-align:center; } a:link{color: #9D943A;font-size:12px;} a:hover{color: #FF3300;font-size:12px;} a:visited{color: #9D943A;font-size:12px;} a.red:link{color: #ff0000;font-size:12px;} a.red:hover{color: #ff0000;font-size:12px;} a.red:visited{color: #ff0000;font-size:12px;} #marqueeBox{background:#f7f7f7;border:1px solid silver;padding:1px;text-align:center;margin:0 auto;} --> </style> </head> <body> <h4>滾動新聞</h4> <script language="JavaScript" type="text/javascript"> var marqueeContent=new Array(); marqueeContent[0]="<a href=http://xyq.163.com/news/2006/11/2-2-20061102170913.html target=_blank>用快速取回帳號密碼</a>"; marqueeContent[1]="<a href=http://ekey.163.com/ target=_blank>網易將軍令官方網站</a>"; marqueeContent[2]="<a href=http://xyq.163.com/download/wallpaper.htm target=_blank>最新壁紙下載</a>"; marqueeContent[3]="<a href=http://xyq.163.com/download/around.htm target=_blank>最新屏保下載</a>"; var marqueeInterval=new Array(); var marqueeId=0; var marqueeDelay=2000; var marqueeHeight=20; function initMarquee() { var str=marqueeContent[0]; document.write('<div id="marqueeBox" style="overflow:hidden;width:250px;height:'+marqueeHeight+'px" onmouseover="clearInterval(marqueeInterval[0])" onmouseout="marqueeInterval[0]=setInterval(\'startMarquee()\',marqueeDelay)"><div>'+str+'</div></div>'); marqueeId++; marqueeInterval[0]=setInterval("startMarquee()",marqueeDelay); } function startMarquee() { var str=marqueeContent[marqueeId]; marqueeId++; if(marqueeId>=marqueeContent.length) marqueeId=0; if(document.getElementById("marqueeBox").childNodes.length==1) { var nextLine=document.createElement('DIV'); nextLine.innerHTML=str; document.getElementById("marqueeBox").appendChild(nextLine); } else { document.getElementById("marqueeBox").childNodes[0].innerHTML=str; document.getElementById("marqueeBox").appendChild(document.getElementById("marqueeBox").childNodes[0]); document.getElementById("marqueeBox").scrollTop=0; } clearInterval(marqueeInterval[1]); marqueeInterval[1]=setInterval("scrollMarquee()",20); } function scrollMarquee() { document.getElementById("marqueeBox").scrollTop++; if(document.getElementById("marqueeBox").scrollTop%marqueeHeight==(marqueeHeight-1)){ clearInterval(marqueeInterval[1]); } } initMarquee(); </script> </body> </html>

也可以用css3實現:

 

 

 

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>無標題文檔</title>
<style>
@keyframes move
{
    0%
    {
        transform:translateY(0px);
    }
50%
    {
        transform:translateY(-200px);
    }
    100%
    {
        transform:translateY(0px);
    }
}
.picTab{height:500px;border:2px solid #000; margin:50px auto;overflow:hidden;}
.picTab .topDiv{width:1000px;height:500px; animation:move 5s linear infinite;text-align:center;}
.picTab div {margin:5px;color:black;  }


</style>
</head>
<body>
<DIV class="picTab">
<div  class="topDiv">
<div>"控制洗衣機"</div>
<div>"控制冰箱"</div>
<div>"開啟洗衣機"</div>
<div>"開始洗衣"</div>
<div>"關閉洗衣機"</div>
<div>"棉麻洗"</div>
<div>"洗滌時間設為20分鍾"</div>
<div>"漂洗2次"</div>
<div>"脫水6分鍾"</div>
<div>"冰箱設為速冷模式"</div>
<div>"冷藏室溫度設為5度"</div>
<div>"天氣"</div>

</div>





</DIV>
</body>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM