效果:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9a7ac78f3c7397c1b6506b936fdc89e1.gif)
原理
- 利用伪类,开始的时候给本来的内容盖住,然后执行动画。
- 设置宽高,宽就是单个内容(比如数字中的一位数)的宽,让内容一行一个的排成列。
- 动画就是让这些内容一个个的往上升,造成一种滚动效果
- 最后露出原来的内容(原来的内容和伪类里的内容是同样的样式)
代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>数字滚动</title>
<style>
.txt {
position: absolute;
left: 178px;
font-size: 30px;
line-height: 30px;
}
.mod_txt {
position: relative;
display: inline-block;
vertical-align: middle;
text-align: center;
float: left;
overflow: hidden;
width: 22px;
height: 30px;
text-transform: uppercase;
font-size: 30px;
color: #333;
}
.mod_txt:before {
position: absolute;
top: 0;
left: 0;
width: 100%;
word-break: break-all;
background: #FFFFFF;
font-size: 30px;
color: #333;
}
.txt11 span:nth-child(1):before {
content: "03456789";
-webkit-animation-name: txt1;
animation-name: txt1;
-webkit-animation-duration: 1.6s;
animation-duration: 1.6s;
-webkit-animation-fill-mode: forwards;
animation-fill-mode: forwards;
}
@-webkit-keyframes txt1 {
from {
margin-top: -648px;
}
to {
margin-top: 110px;
}
}
.txt11 span:nth-child(2):before {
content: "051341989";
-webkit-animation-name: txt1;
animation-name: txt1;
-webkit-animation-duration: 1.8s;
animation-duration: 1.8s;
-webkit-animation-fill-mode: forwards;
animation-fill-mode: forwards;
}
</style>
</head>
<body>
<div class="txt txt11">
<span class="mod_txt">2</span>
<span class="mod_txt">0</span>
</div>
</body>
</html>
唠唠叨叨
- 想让动画停留的时间不一样,就要让
animation-duration
不一样 - 想让动画开始运动的时间不同,就单加
animation-delay
,但这样会让原来的数字先被看到一会 - 想先不让动画执行,到一定时间了再执行的话,以上面的demo为例,html里可以
先不写class名txt11
,等到了时候再$('.txt').addClass('txt11');
就好了