DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML5詳解
CSS3+HTML5特效:純CSS縱向無縫滾動
編輯:HTML5詳解     

老規矩,先看效果。下面這段文字會持續向上滾動,滑鼠移入時還會自動暫停:

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 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程式碼說明:

  1. @-webkit-keyframes與@keyframes定義了從0%到100%之間,每經過20%的時間就向上移動30px,總共移動6次;
  2. .box3定義外容器的基本屬性;
  3. .border3定義內容器的屬性,其中animation設定為8秒循環一次、無限播放並套用指定的緩動曲線;
  4. .border3 div定義縱向滾動內容的基本樣式;
  5. .border3:hover定義滑鼠移入時的效果,以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條刪掉,就能看到差別了。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved