文字逐行显示效果
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>使用原生js实现文字逐行显示</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
div {
position: relative;
margin: 40px;
width: 300px;
height: 40px;
border: 2px solid red;
}
ol {
position: absolute;
}
li {
font-size: 20px;
height: 40px;
list-style: none;
line-height: 40px;
}
li:nth-child(2n) {
background: pink;
}
li.demo{
color: blue;
font-size: 30px;
}
</style>
</head>
<body>
<div id="box">
<ol>
<li class="demo">1111111111111111</li>
<li>22222222222222</li>
<li>333333333333333</li>
<li>444444444444444</li>
<li>555555555555555</li>
<li>666666666666666</li>
</ol>
</div>
<script type="text/javascript">
var box = document.getElementById("box");
var ol=document.getElementsByTagName("ol")[0]
var li=document.getElementsByTagName("li");
var speed=0;
var order=true
var timer =playTimer= null;
var index=0;
timer=setInterval(startMove,2000);
function startMove(){
indexChange();
}
function indexChange(){
order?index++:index--;
if(index<=0){
index=0;
order=true;
}
if(index>=li.length-1){
index=li.length-1;
order=false
}
init()
}
function init(){
for(var i=0;i<li.length;i++){
li[i].className="";
li[index].className="demo";
textMove(-index*li[index].offsetHeight);
}
}
function textMove(target){
clearInterval(playTimer)
playTimer=setInterval(function(){
speed = (target-ol.offsetTop)/10;
speed =speed>0?Math.ceil(speed):Math.floor(speed)
if(target==ol.offsetHeight){
clearInterval(playTimer)
}else{
ol.style.top = ol.offsetTop + speed + "px";
}
},30)
}
</script>
</body>
</html>
文字滚动显示效果
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>滚动显示效果</title>
<style type="text/css">
div{
border: 1px solid red;
width: 90%;
}
p{
white-space: pre;
}
</style>
</head>
<body>
<div>
<p>1 23 4 56</p>
</div>
<script type="text/javascript">
var p=document.getElementsByTagName("p")[0];
var newStr=null;
var timer=null;
var str;
timer=setInterval(function(){
str=p.innerHTML;
newStr = str.substr(1)+str.substr(0,1);
p.innerHTML= newStr
console.log(str)
},1000)
</script>
</body>
</html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了