[转载]CSS3+HTML5特效3 – 纵向无缝滚动 – z_gia – 博客园.
老惯例,先看例子。
This is a test 1.
This is a test 2.
This is a test 3.
This is a test 4.
This is a test 5.
This is a test 1.
实现原理:
1. 利用CSS3的@keyframes规则创建动画效果;
2. 使用CSS3的animation效果完成滚动切换。
CSS代码
@-webkit-keyframes scrollText 1 { 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 scrollText 1 { 0% { transform: translateY( 0px ); } 20% { transform: translateY( -30px ); } 40% { transform: translateY( -60px ); } 60% { transform: translateY( -90px ); } 80% { transform: translateY( -120px ); } 100% { transform: translateY( -150px ); } } .box 3 { position : relative ; top : 20px ; left : 20px ; width : 200px ; height : 30px ; overflow : hidden ; border : 1px solid #ccc ; } .border 3 { top : 0px ; -webkit-animation:scrollText 1 8 s infinite cubic-bezier( 1 , 0 , 0.5 , 0 ) ; animation:scrollText 1 8 s infinite cubic-bezier( 1 , 0 , 0.5 , 0 ) ; } .border 3 div{ height : 30px ; } .border 3: 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 > |
HTML代码说明:
定义了6条信息可以纵向滚动,其中前5条是真正纵向滚动的信息,第6条和第1条信息是一样的,原因很简单,因为使用了@keyframes方式来实现动画效果,第1条信息的效果是默认为停止的,所以用第6条信息制作一个替代方法,在第一次循环结束后,可以无缝继续滚动,大家可以去掉第6条试一下,就可以看见效果了。
至此,大功告成。