实现原理:
-
利用CSS3的@keyframes规则创建动画效果;
-
使用CSS3的animation效果完成滚动切换。
-
CSS代码
@-webkit-keyframes scrollText1 {
0%{
-webkit-transform: translateY(0px);
}
20%{
-webkit-transform: translateY(-30px);
}
40%{
-webkit-transform: translateY(-60px);
}
60%{
-webkit-transform: translateY(-90px);
}
80%{
-webkit-transform: translateY(-120px);
}
100%{
-webkit-transform: translateY(-150px);
}
}
@keyframes scrollText1 {
0%{
transform: translateY(0px);
}
20%{
transform: translateY(-30px);
}
40%{
transform: translateY(-60px);
}
60%{
transform: translateY(-90px);
}
80%{
transform: translateY(-120px);
}
100%{
transform: translateY(-150px);
}
}
.box3{
position: relative;
top: 20px;
left: 20px;
width: 200px;
height: 30px;
overflow: hidden;
border:1px solid #ccc;
}
.border3{
top: 0px;
-webkit-animation:scrollText1 8s infinite cubic-bezier(1,0,0.5,0) ;
animation:scrollText1 8s infinite cubic-bezier(1,0,0.5,0) ;
}
.border3 div{
height: 30px;
}
.border3:hover{
animation-play-state:paused;
-webkit-animation-play-state:paused;
}
CSS代码说明:
@-webkit-keyframes及@keyframes定义了从0% ~ 100%之间,每过20%的时间,向上移动30px,总共有6次移动;
.box3 定义外容器的基本属性
.border3 定义了内容器的属性,-webkit-animation:scrollText1 8s infinite cubic-bezier(1,0,0.5,0) 和 animation:scrollText1 8s infinite cubic-bezier(1,0,0.5,0) 定义了用8s种循环一次,无限循环已经移动是的效果;
.border3 div 定义了纵向滚动内容的基本样式;
.border3:hover 定义了鼠标移入容器是的效果,animation-play-state:paused 及 -webkit-animation-play-state:paused 定义了动画暂停;
HTML代码
<div class="box3">
<div class="border3">
<div>This is a test 1.</div>
<div>This is a test 2.</div>
<div>This is a test 3.</div>
<div>This is a test 4.</div>
<div>This is a test 5.</div>
<div>This is a test 1.</div>
</div>
</div>