CSS3 動(dòng)畫
CSS3 動(dòng)畫
通過 CSS3,我們能夠創(chuàng)建動(dòng)畫,這可以在許多網(wǎng)頁中取代動(dòng)畫圖片、Flash 動(dòng)畫以及 JavaScript。
CSS3 動(dòng)畫
CSS3 @keyframes 規(guī)則
如需在 CSS3 中創(chuàng)建動(dòng)畫,您需要學(xué)習(xí) @keyframes 規(guī)則。
@keyframes 規(guī)則用于創(chuàng)建動(dòng)畫。在 @keyframes 中規(guī)定某項(xiàng) CSS 樣式,就能創(chuàng)建由當(dāng)前樣式逐漸改為新樣式的動(dòng)畫效果。
瀏覽器支持
| 屬性 | 瀏覽器支持 | ||||
|---|---|---|---|---|---|
| @keyframes | |||||
| animation | |||||
Internet Explorer 10、Firefox 以及 Opera 支持 @keyframes 規(guī)則和 animation 屬性。
Chrome 和 Safari 需要前綴 -webkit-。
注釋:Internet Explorer 9,以及更早的版本,不支持 @keyframe 規(guī)則或 animation 屬性。
實(shí)例
@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;}
}
CSS3 動(dòng)畫
當(dāng)您在 @keyframes 中創(chuàng)建動(dòng)畫時(shí),請把它捆綁到某個(gè)選擇器,否則不會(huì)產(chǎn)生動(dòng)畫效果。
通過規(guī)定至少以下兩項(xiàng) CSS3 動(dòng)畫屬性,即可將動(dòng)畫綁定到選擇器:
- 規(guī)定動(dòng)畫的名稱
- 規(guī)定動(dòng)畫的時(shí)長
實(shí)例
把 "myfirst" 動(dòng)畫捆綁到 div 元素,時(shí)長:5 秒:
div
{
animation: myfirst 5s;
-moz-animation: myfirst 5s; /* Firefox */
-webkit-animation: myfirst 5s; /* Safari 和 Chrome */
-o-animation: myfirst 5s; /* Opera */
}
注釋:您必須定義動(dòng)畫的名稱和時(shí)長。如果忽略時(shí)長,則動(dòng)畫不會(huì)允許,因?yàn)槟J(rèn)值是 0。
什么是 CSS3 中的動(dòng)畫?
動(dòng)畫是使元素從一種樣式逐漸變化為另一種樣式的效果。
您可以改變?nèi)我舛嗟臉邮饺我舛嗟拇螖?shù)。
請用百分比來規(guī)定變化發(fā)生的時(shí)間,或用關(guān)鍵詞 "from" 和 "to",等同于 0% 和 100%。
0% 是動(dòng)畫的開始,100% 是動(dòng)畫的完成。
為了得到最佳的瀏覽器支持,您應(yīng)該始終定義 0% 和 100% 選擇器。
實(shí)例
當(dāng)動(dòng)畫為 25% 及 50% 時(shí)改變背景色,然后當(dāng)動(dòng)畫 100% 完成時(shí)再次改變:
@keyframes myfirst
{
0% {background: red;}
25% {background: yellow;}
50% {background: blue;}
100% {background: green;}
}
@-moz-keyframes myfirst /* Firefox */
{
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;}
}
@-o-keyframes myfirst /* Opera */
{
0% {background: red;}
25% {background: yellow;}
50% {background: blue;}
100% {background: green;}
}
實(shí)例
改變背景色和位置:
@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;}
}
@-moz-keyframes myfirst /* Firefox */
{
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;}
}
@-o-keyframes myfirst /* Opera */
{
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 動(dòng)畫屬性
下面的表格列出了 @keyframes 規(guī)則和所有動(dòng)畫屬性:
| 屬性 | 描述 | CSS |
|---|---|---|
| @keyframes | 規(guī)定動(dòng)畫。 | 3 |
| animation | 所有動(dòng)畫屬性的簡寫屬性,除了 animation-play-state 屬性。 | 3 |
| animation-name | 規(guī)定 @keyframes 動(dòng)畫的名稱。 | 3 |
| animation-duration | 規(guī)定動(dòng)畫完成一個(gè)周期所花費(fèi)的秒或毫秒。默認(rèn)是 0。 | 3 |
| animation-timing-function | 規(guī)定動(dòng)畫的速度曲線。默認(rèn)是 "ease"。 | 3 |
| animation-delay | 規(guī)定動(dòng)畫何時(shí)開始。默認(rèn)是 0。 | 3 |
| animation-iteration-count | 規(guī)定動(dòng)畫被播放的次數(shù)。默認(rèn)是 1。 | 3 |
| animation-direction | 規(guī)定動(dòng)畫是否在下一周期逆向地播放。默認(rèn)是 "normal"。 | 3 |
| animation-play-state | 規(guī)定動(dòng)畫是否正在運(yùn)行或暫停。默認(rèn)是 "running"。 | 3 |
| animation-fill-mode | 規(guī)定對象動(dòng)畫時(shí)間之外的狀態(tài)。 | 3 |
下面的兩個(gè)例子設(shè)置了所有動(dòng)畫屬性:
實(shí)例
運(yùn)行名為 myfirst 的動(dòng)畫,其中設(shè)置了所有動(dòng)畫屬性:
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;
}
實(shí)例
與上面的動(dòng)畫相同,但是使用了簡寫的動(dòng)畫 animation 屬性:
div
{
animation: myfirst 5s linear 2s infinite alternate;
/* Firefox: */
-moz-animation: myfirst 5s linear 2s infinite alternate;
/* Safari 和 Chrome: */
-webkit-animation: myfirst 5s linear 2s infinite alternate;
/* Opera: */
-o-animation: myfirst 5s linear 2s infinite alternate;
}