有了 CSS3 動畫,我們可以在樣式表裡直接做出時間軸動效,這在許多場合已經足以取代 GIF 動圖、Flash 動畫,甚至部分原本需要 JavaScript 才能完成的互動。
先看一個範例:載入頁面後,方塊會變色、轉動並左右移動。請用 IE10 以上(IE9 及以下不支援)、Chrome、Firefox 或 Opera 查看。
要在 CSS3 中建立動畫,核心就是 @keyframes。你在 @keyframes 裡定義某個元素在各時間點的樣子,瀏覽器就會自動把中間的變化補上,形成一段由起始樣式過渡到結束樣式的動畫。
瀏覽器支援方面,Internet Explorer 10、Firefox 與 Opera 支援標準的 @keyframes 與 animation;Chrome 與 Safari 需要加上 -webkit- 前綴。IE9 及更早版本完全不支援。
@keyframes myfirst {
from { background: red; }
to { background: yellow; }
}
@-moz-keyframes myfirst /* Firefox */
{
from { background: red; }
to { background: yellow; }
}
@-webkit-keyframes myfirst /* Safari 與 Chrome */
{
from { background: red; }
to { background: yellow; }
}
@-o-keyframes myfirst /* Opera */
{
from { background: red; }
to { background: yellow; }
}
定義好 @keyframes 之後,還必須把它綁到某個選擇器上,否則不會有任何動畫發生。綁定時至少要指定兩件事:動畫的名稱,以及動畫的時長。
div {
animation: myfirst 5s;
-moz-animation: myfirst 5s; /* Firefox */
-webkit-animation: myfirst 5s; /* Safari 與 Chrome */
-o-animation: myfirst 5s; /* Opera */
}
務必定義名稱與時長。如果省略時長,動畫不會播放,因為預設值是 0 秒。
所謂 CSS3 動畫,就是讓元素從一種樣式逐漸變化為另一種樣式,而且你可以改變任意多的屬性、任意多次。時間點用百分比表示,也可以使用 from 與 to 這兩個關鍵字,它們等同於 0% 與 100%。0% 是動畫的起點,100% 是終點。
為了取得最好的相容性,建議永遠明確寫出 0% 與 100% 兩個狀態。
@keyframes myfirst {
0% { background: red; }
25% { background: yellow; }
50% { background: blue; }
100% { background: green; }
}
也可以同時改變多個屬性,例如讓方塊沿著正方形路線移動並變色:
@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; }
}
(實作時記得為 Firefox、Safari/Chrome、Opera 各補一份帶前綴的 @-moz-keyframes、@-webkit-keyframes、@-o-keyframes,內容完全相同。)
・@keyframes:定義動畫本身(CSS3)
・animation:除 animation-play-state 外所有動畫屬性的簡寫
・animation-name:指定要使用的 @keyframes 名稱
・animation-duration:完成一個週期所需時間,單位為秒或毫秒,預設 0
・animation-timing-function:速度曲線,預設 ease
・animation-delay:延遲多久開始,預設 0
・animation-iteration-count:播放次數,預設 1
・animation-direction:下一個週期是否逆向播放,預設 normal
・animation-play-state:暫停或執行中,預設 running
逐一列出所有屬性的寫法:
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;
/* Firefox */
-moz-animation-name: myfirst;
-moz-animation-duration: 5s;
-moz-animation-timing-function: linear;
-moz-animation-delay: 2s;
-moz-animation-iteration-count: infinite;
-moz-animation-direction: alternate;
-moz-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;
/* Opera */
-o-animation-name: myfirst;
-o-animation-duration: 5s;
-o-animation-timing-function: linear;
-o-animation-delay: 2s;
-o-animation-iteration-count: infinite;
-o-animation-direction: alternate;
-o-animation-play-state: running;
}
同樣的效果,用簡寫屬性只要一行:
div {
animation: myfirst 5s linear 2s infinite alternate;
-moz-animation: myfirst 5s linear 2s infinite alternate;
-webkit-animation: myfirst 5s linear 2s infinite alternate;
-o-animation: myfirst 5s linear 2s infinite alternate;
}
這段設定的意思是:播放名為 myfirst 的動畫,每次 5 秒、線性速度、延遲 2 秒開始、無限次、每次往返交替播放。
第一,能用 transform 與 opacity 就不要用 left/top/width。前者可以交由合成器處理,不觸發重排(reflow),在行動裝置上明顯更順;範例為了示範單純易懂才使用 left。
第二,動畫不該只有好看。若使用者有大幅晃動落落暈眩的困擾,應搭配 prefers-reduced-motion 媒體查詢提供關閉動畫的選項,這是無障礙設計的基本禮貌。