CSS3中Animation動(dòng)畫屬性用法詳解

要使用animation動(dòng)畫,先要熟悉一下keyframes,Keyframes的語法規(guī)則:命名是由”@keyframes”開頭,后面緊接著是這個(gè)“動(dòng)畫的名稱”加上一對(duì)花括號(hào)“{}”,括號(hào)中就是一些不同時(shí)間段樣式規(guī)則。不同關(guān)鍵幀是通過from(相當(dāng)于0%)、to(相當(dāng)于100%)或百分比來表示(為了得到最佳的瀏覽器支持,建議使用百分比),如下定義一個(gè)簡(jiǎn)單的動(dòng)畫:
- @keyframes myfirst /*定義動(dòng)畫名*/
- {
- 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把它綁定到一個(gè)選擇器,否則動(dòng)畫不會(huì)有任何效果。下面列出了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;
上述所有代碼可以如下簡(jiǎn)寫:
- 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 /*定義動(dòng)畫名*/
- {
- 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>該實(shí)例在 Internet Explorer 9 及更早 IE 版本是無效的。</p>
- <div></div>
- </body>
- </html>
上面代碼演示了一個(gè)正方形沿著一個(gè)正方形軌跡運(yùn)動(dòng),基數(shù)次按正方向運(yùn)動(dòng),偶數(shù)次按反方向運(yùn)動(dòng),運(yùn)動(dòng)過程中還帶有顏色變化。具體效果,讀者可以自行運(yùn)行代碼觀察。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
CSS3動(dòng)畫之利用requestAnimationFrame觸發(fā)重新播放功能
這篇文章主要介紹了利用requestAnimationFrame重新播放(觸發(fā))CSS3動(dòng)畫,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-11CSS3 animation – steps 函數(shù)詳解
本文通過實(shí)例代碼給大家介紹了CSS3 animation – steps 函數(shù),代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-08-30css3中用animation的steps屬性制作幀動(dòng)畫
這篇文章主要介紹了css中用animation的steps屬性制作幀動(dòng)畫,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-25- 本篇介紹的animation屬性和傳統(tǒng)的動(dòng)畫制作一樣,能控制幀的每一步,制作出更強(qiáng)大的動(dòng)畫效果。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看2018-12-25
css3的動(dòng)畫特效之動(dòng)畫序列(animation)
這篇文章主要介紹了css3的動(dòng)畫特效之動(dòng)畫序列(animation) 的相關(guān)資料,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-22CSS3中animation實(shí)現(xiàn)流光按鈕效果
這篇文章主要介紹了CSS3中animation實(shí)現(xiàn)流光按鈕效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì)對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-21