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>

猜你喜欢

转载自www.cnblogs.com/yanyanstyle/p/11287342.html