CSS3 動畫

CSS3 動畫

CSS3 可以建立動畫,它可以取代許多網頁動畫影象、Flash 動畫和 JavaScript 實現的效果。


CSS3
動畫

CSS3 @keyframes 規則

要建立 CSS3 動畫,你需要了解 @keyframes 規則。

@keyframes 規則是建立動畫。

@keyframes 規則內指定一個 CSS 樣式和動畫將逐步從目前的樣式更改為新的樣式。


瀏覽器支援

表格中的數字表示支援該屬性的第一個瀏覽器版本號。

緊跟在 -webkit-, -ms- 或 -moz- 前的數字為支援該字首屬性的第一個瀏覽器版本號。

屬性
@keyframes 43.0
4.0 -webkit-
10.0 16.0
5.0 -moz-
9.0
4.0 -webkit-
30.0
15.0 -webkit-
12.0 -o-
animation 43.0
4.0 -webkit-
10.0 16.0
5.0 -moz-
9.0
4.0 -webkit-
30.0
15.0 -webkit-
12.0 -o-

OperaSafariChromeFirefoxInternet Explorer

例項

@keyframes myfirst { from {background: red;} to {background: yellow;} } @-webkit-keyframes myfirst /* Safari 與 Chrome */ { from {background: red;} to {background: yellow;} }


CSS3 動畫

當在 @keyframes 建立動畫,把它繫結到一個選擇器,否則動畫不會有任何效果。

指定至少這兩個CSS3的動畫屬性綁定向一個選擇器:

  • 規定動畫的名稱
  • 規定動畫的時長
OperaSafariChromeFirefoxInternet Explorer

例項

把 "myfirst" 動畫捆綁到 div 元素,時長:5 秒:

div { animation: myfirst 5s; -webkit-animation: myfirst 5s; /* Safari 與 Chrome */ }

嘗試一下 ?

注意: 您必須定義動畫的名稱和動畫的持續時間。如果省略的持續時間,動畫將無法執行,因為預設值是0。


CSS3動畫是什麼?

動畫是使元素從一種樣式逐漸變化為另一種樣式的效果。

您可以改變任意多的樣式任意多的次數。

請用百分比來規定變化發生的時間,或用關鍵詞 "from" 和 "to",等同於 0% 和 100%。

0% 是動畫的開始,100% 是動畫的完成。

為了得到最佳的瀏覽器支援,您應該始終定義 0% 和 100% 選擇器。

OperaSafariChromeFirefoxInternet Explorer

例項

當動畫為 25% 及 50% 時改變背景色,然後當動畫 100% 完成時再次改變:

@keyframes myfirst { 0% {background: red;} 25% {background: yellow;} 50% {background: blue;} 100% {background: green;} } @-webkit-keyframes myfirst /* Safari 與 Chrome */ { 0% {background: red;} 25% {background: yellow;} 50% {background: blue;} 100% {background: green;} }

嘗試一下 ?

OperaSafariChromeFirefoxInternet Explorer

例項

改變背景色和位置:

@keyframes myfirst { 0% {background: red; left:0px; top:0px;} 25% {background: yellow; left:200px; top:0px;} 50% {background: blue; left:200px; top:200px;} 75% {background: green; left:0px; top:200px;} 100% {background: red; left:0px; top:0px;} } @-webkit-keyframes myfirst /* Safari 與 Chrome */ { 0% {background: red; left:0px; top:0px;} 25% {background: yellow; left:200px; top:0px;} 50% {background: blue; left:200px; top:200px;} 75% {background: green; left:0px; top:200px;} 100% {background: red; left:0px; top:0px;} }

嘗試一下 ?


CSS3的動畫屬性

下面的表格列出了 @keyframes 規則和所有動畫屬性:

屬性 描述 CSS
@keyframes 規定動畫。 3
animation 所有動畫屬性的簡寫屬性。 3
animation-name 規定 @keyframes 動畫的名稱。 3
animation-duration 規定動畫完成一個週期所花費的秒或毫秒。預設是 0。 3
animation-timing-function 規定動畫的速度曲線。預設是 "ease"。 3
animation-fill-mode 規定當動畫不播放時(當動畫完成時,或當動畫有一個延遲未開始播放時),要應用到元素的樣式。 3
animation-delay 規定動畫何時開始。預設是 0。 3
animation-iteration-count 規定動畫被播放的次數。預設是 1。 3
animation-direction 規定動畫是否在下一週期逆向地播放。預設是 "normal"。 3
animation-play-state 規定動畫是否正在執行或暫停。預設是 "running"。 3

下面兩個例子設定所有動畫屬性:

OperaSafariChromeFirefoxInternet Explorer

例項

執行myfirst動畫,設定所有的屬性:

div { animation-name: myfirst; animation-duration: 5s; animation-timing-function: linear; animation-delay: 2s; animation-iteration-count: infinite; animation-direction: alternate; animation-play-state: running; /* Safari 與 Chrome: */ -webkit-animation-name: myfirst; -webkit-animation-duration: 5s; -webkit-animation-timing-function: linear; -webkit-animation-delay: 2s; -webkit-animation-iteration-count: infinite; -webkit-animation-direction: alternate; -webkit-animation-play-state: running; }

嘗試一下 ?

OperaSafariChromeFirefoxInternet Explorer

例項

與上面的動畫相同,但是使用了簡寫的動畫 animation 屬性:

div { animation: myfirst 5s linear 2s infinite alternate; /* Safari 與 Chrome: */ -webkit-animation: myfirst 5s linear 2s infinite alternate; }

嘗試一下 ?