CSS3中Animation動畫屬性用法詳解

要使用animation動畫,先要熟悉一下keyframes,Keyframes的語法規(guī)則:命名是由”@keyframes”開頭,后面緊接著是這個“動畫的名稱”加上一對花括號“{}”,括號中就是一些不同時間段樣式規(guī)則。不同關(guān)鍵幀是通過from(相當(dāng)于0%)、to(相當(dāng)于100%)或百分比來表示(為了得到最佳的瀏覽器支持,建議使用百分比),如下定義一個簡單的動畫:
- @keyframes myfirst /*定義動畫名*/
- {
- 0% {background:red; left:0px; top:0px;} /*定義起始幀樣式,0%可以換成from*/
- 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;} /*定義結(jié)束幀樣式,100%可以換成to*/
- }
@keyframes定義好了,要使其能發(fā)揮效果,必須通過animation把它綁定到一個選擇器,否則動畫不會有任何效果。下面列出了animation的屬性:
下面設(shè)置上述的所有屬性
- animation-name:myfirst;
- animation-duration:5s;
- animation-timing-function:linear;
- animation-delay:1s;
- animation-iteration-count:infinite;
- animation-direction:alternate;
- animation-play-state:running;
上述所有代碼可以如下簡寫:
- animation:myfirst 5s linear 2s infinite alternate;
- animation-play-state:running;
瀏覽器兼容性
Internet Explorer 10、Firefox 以及 Opera 支持 @keyframes 規(guī)則和 animation 屬性。
Chrome 和 Safari 需要前綴 -webkit-。
注意:Internet Explorer 9,以及更早的版本,不支持 @keyframe 規(guī)則或 animation 屬性。
下面給出上面介紹的關(guān)于keyframes和animation屬性的完整代碼示例:
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>animation演示</title>
- <style>
- div
- {
- width:100px;
- height:100px;
- background:red;
- position:relative;
- animation-name:myfirst;
- animation-duration:5s;
- animation-timing-function:linear;
- animation-delay:1s;
- animation-iteration-count:infinite;
- animation-direction:alternate;
- animation-play-state:running;
- /* Safari and Chrome: */
- -webkit-animation-name:myfirst;
- -webkit-animation-duration:5s;
- -webkit-animation-timing-function:linear;
- -webkit-animation-delay:1s;
- -webkit-animation-iteration-count:infinite;
- -webkit-animation-direction:alternate;
- -webkit-animation-play-state:running;
- }
- @keyframes myfirst /*定義動畫名*/
- {
- 0% {background:red; left:0px; top:0px;} /*定義起始幀樣式,0%相當(dāng)于from*/
- 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;} /*定義結(jié)束幀樣式,100%相當(dāng)于to*/
- }
- @-webkit-keyframes myfirst /* Safari and 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;}
- }
- </style>
- </head>
- <body>
- <p>該實例在 Internet Explorer 9 及更早 IE 版本是無效的。</p>
- <div></div>
- </body>
- </html>
上面代碼演示了一個正方形沿著一個正方形軌跡運動,基數(shù)次按正方向運動,偶數(shù)次按反方向運動,運動過程中還帶有顏色變化。具體效果,讀者可以自行運行代碼觀察。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
CSS3動畫之利用requestAnimationFrame觸發(fā)重新播放功能
這篇文章主要介紹了利用requestAnimationFrame重新播放(觸發(fā))CSS3動畫,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-11CSS3 animation – steps 函數(shù)詳解
本文通過實例代碼給大家介紹了CSS3 animation – steps 函數(shù),代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧2019-08-30- 這篇文章主要介紹了css中用animation的steps屬性制作幀動畫,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-25
- 本篇介紹的animation屬性和傳統(tǒng)的動畫制作一樣,能控制幀的每一步,制作出更強大的動畫效果。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看2018-12-25
- 這篇文章主要介紹了css3的動畫特效之動畫序列(animation) 的相關(guān)資料,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-22
- 這篇文章主要介紹了CSS3中animation實現(xiàn)流光按鈕效果,本文通過實例代碼給大家介紹的非常詳細(xì)對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-21