Internet Explorer 十、Firefox 以及 Opera 支持 @keyframes 規則和 animation 屬性。css
Chrome 和 Safari 須要前綴 -webkit-。web
Internet Explorer 9,以及更早的版本,不支持 @keyframe 規則或 animation 屬性。動畫
當您在 @keyframes 中建立動畫時,請把它捆綁到某個選擇器,不然不會產生動畫效果。spa
經過規定至少如下兩項 CSS3 動畫屬性,便可將動畫綁定到選擇器:ssr
例子:code
當動畫爲 25% 及 50% 時改變背景色,而後當動畫 100% 完成時再次改變:orm
div
{
animation: myfirst 5s; /*IE*/
-moz-animation: myfirst 5s; /* Firefox */
-webkit-animation: myfirst 5s; /* Safari 和 Chrome */ -o-animation: myfirst 5s; /* Opera */ }
@keyframes myfirst /*IE*/
{
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;} }
改變背景色和位置:
@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;} }
下面的表格列出了 @keyframes 規則和全部動畫屬性:對象
屬性 | 描述 | CSS |
---|---|---|
@keyframes | 規定動畫。 | 3 |
animation | 全部動畫屬性的簡寫屬性,除了 animation-play-state 屬性。 | 3 |
animation-name | 規定 @keyframes 動畫的名稱。 | 3 |
animation-duration | 規定動畫完成一個週期所花費的秒或毫秒。默認是 0。 | 3 |
animation-timing-function | 規定動畫的速度曲線。默認是 "ease"。 | 3 |
animation-delay | 規定動畫什麼時候開始。默認是 0。 | 3 |
animation-iteration-count | 規定動畫被播放的次數。默認是 1。 | 3 |
animation-direction | 規定動畫是否在下一週期逆向地播放。默認是 "normal"。 | 3 |
animation-play-state | 規定動畫是否正在運行或暫停。默認是 "running"。 | 3 |
animation-fill-mode | 規定對象動畫時間以外的狀態。 | 3 |
運行名爲 myfirst 的動畫,其中設置了全部動畫屬性:get
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; }