CSS3 動畫屬性
CSS3新增動畫屬性“@keyframes”,從字面就可以看出其含義——關(guān)鍵幀,這與Flash中的含義一致。利用CSS3制作動畫效果其原理與Flash一樣,我們需要定義關(guān)鍵幀處的狀態(tài)效果,由CSS3來驅(qū)動產(chǎn)生動畫效果。
語法
@keyframes?animationname {keyframes-selector {css-styles;}}
animationname 必需。定義動畫的名稱。
keyframes-selector
必需。動畫時長的百分比。
合法的值:
0-100%
from(與 0% 相同)
to(與 100% 相同)
css-styles 必需。一個或多個合法的 CSS 樣式屬性。
定義和用法
通過?@keyframes?規(guī)則,您能夠創(chuàng)建動畫。
創(chuàng)建動畫的原理是,將一套 CSS 樣式逐漸變化為另一套樣式。
在動畫過程中,您能夠多次改變這套 CSS 樣式。
以百分比來規(guī)定改變發(fā)生的時間,或者通過關(guān)鍵詞 "from" 和 "to",等價于 0% 和 100%。
0% 是動畫的開始時間,100% 動畫的結(jié)束時間。
為了獲得最佳的瀏覽器支持,您應該始終定義 0% 和 100% 選擇器。
注釋:請使用動畫屬性來控制動畫的外觀,同時將動畫與選擇器綁定。
瀏覽器支持情況
目前瀏覽器都不支持?@keyframes?規(guī)則。
Firefox 支持替代的 @-moz-keyframes?規(guī)則。
Opera 支持替代的 @-o-keyframes?規(guī)則。
Safari 和 Chrome 支持替代的 @-webkit-keyframes?規(guī)則。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style type="text/css"> div { width: 100px; height: 100px; background: #ff72cc; position: relative; animation: mymove 5s infinite; -moz-animation: mymove 5s infinite; /* Firefox */ -webkit-animation: mymove 5s infinite; /* Safari and Chrome */ -o-animation: mymove 5s infinite; /* Opera */ } @keyframes mymove { 0% { top: 0px; } 25% { top: 200px; } 75% { top: 50px } 100% { top: 100px; } } @-moz-keyframes mymove /* Firefox */ { 0% { top: 0px; } 25% { top: 200px; } 75% { top: 50px } 100% { top: 100px; } } @-webkit-keyframes mymove /* Safari and Chrome */ { 0% { top: 0px; } 25% { top: 200px; } 75% { top: 50px } 100% { top: 100px; } } @-o-keyframes mymove /* Opera */ { 0% { top: 0px; } 25% { top: 200px; } 75% { top: 50px } 100% { top: 100px; } } </style> </head> <body> <div></div> </body> </html>
CSS3 動畫
當在?@keyframes?創(chuàng)建動畫,把它綁定到一個選擇器,否則動畫不會有任何效果。
指定至少這兩個CSS3的動畫屬性綁定向一個選擇器:
規(guī)定動畫的名稱
規(guī)定動畫的時長
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style type="text/css"> div { width: 100px; height: 100px; background: red; position: relative; animation: mymove 5s infinite; -moz-animation: mymove 5s infinite; /* Firefox */ -webkit-animation: mymove 5s infinite; /* Safari and Chrome */ -o-animation: mymove 5s infinite; /* Opera */ } @keyframes mymove { 0% { top: 0px; background: red; width: 100px; } 100% { top: 200px; background: yellow; width: 300px; } } @-moz-keyframes mymove /* Firefox */ { 0% { top: 0px; background: red; width: 100px; } 100% { top: 200px; background: yellow; width: 300px; } } @-webkit-keyframes mymove /* Safari and Chrome */ { 0% { top: 0px; background: red; width: 100px; } 100% { top: 200px; background: yellow; width: 300px; } } @-o-keyframes mymove /* Opera */ { 0% { top: 0px; background: red; width: 100px; } 100% { top: 200px; background: yellow; width: 300px; } } </style> </head> <body> <p><b>注釋:</b>本例在 Internet Explorer 中無效。</p> <div></div> </body> </html>
CSS3的動畫屬性
下面的表格列出了 @keyframes 規(guī)則和所有動畫屬性:
屬性 ? ? ? ??描述 ? ? ? ? ? ?CSS
@keyframes ? ?規(guī)定動畫。 ? ?3 ? ?
animation ? ?所有動畫屬性的簡寫屬性,除了 animation-play-state 屬性。 ? ?3 ? ?
animation-name ? ?規(guī)定 @keyframes 動畫的名稱。 ? ?3 ? ?
animation-duration ? ?規(guī)定動畫完成一個周期所花費的秒或毫秒。默認是 0。 ? ?3 ? ?
animation-timing-function ? ?規(guī)定動畫的速度曲線。默認是 "ease"。 ? ?3 ? ?
animation-delay ? ?規(guī)定動畫何時開始。默認是 0。 ? ?3 ? ?
animation-iteration-count ? ?規(guī)定動畫被播放的次數(shù)。默認是 1。 ? ?3 ? ?
animation-direction ? ?規(guī)定動畫是否在下一周期逆向地播放。默認是 "normal"。 ? ?3 ? ?
animation-play-state ? ?規(guī)定動畫是否正在運行或暫停。默認是 "running"。 ? ?3 ? ?
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style type="text/css"> div { width: 100px; height: 100px; background: red; position: relative; animation: mymove 5s infinite; -moz-animation: mymove 5s infinite; /* Firefox */ -webkit-animation: mymove 5s infinite; /* Safari and Chrome */ -o-animation: mymove 5s infinite; /* Opera */ } @keyframes mymove { 0% { top: 0px; left: 0px; background: red; } 25% { top: 0px; left: 100px; background: blue; } 50% { top: 100px; left: 100px; background: yellow; } 75% { top: 100px; left: 0px; background: green; } 100% { top: 0px; left: 0px; background: red; } } @-moz-keyframes mymove /* Firefox */ { 0% { top: 0px; left: 0px; background: red; } 25% { top: 0px; left: 100px; background: blue; } 50% { top: 100px; left: 100px; background: yellow; } 75% { top: 100px; left: 0px; background: green; } 100% { top: 0px; left: 0px; background: red; } } @-webkit-keyframes mymove /* Safari and Chrome */ { 0% { top: 0px; left: 0px; background: red; } 25% { top: 0px; left: 100px; background: blue; } 50% { top: 100px; left: 100px; background: yellow; } 75% { top: 100px; left: 0px; background: green; } 100% { top: 0px; left: 0px; background: red; } } @-o-keyframes mymove /* Opera */ { 0% { top: 0px; left: 0px; background: red; } 25% { top: 0px; left: 100px; background: blue; } 50% { top: 100px; left: 100px; background: yellow; } 75% { top: 100px; left: 0px; background: green; } 100% { top: 0px; left: 0px; background: red; } } </style> </head> <body> <p><b>注釋:</b>本例在 Internet Explorer 中無效。</p> <div></div> </body> </html>