參考:https://www.qianduan.net/css3-animation/
利用css3-animation來制作逐幀動畫
常見用法:
:hover{ animation:mymove 4s ease-out 1s backwards;}
@-webkit-keyframes mymove /*Safari and Chrome*/ { from {left:0px;} to {left:200px;} }
解釋:
mymove :keyframes的名稱;
4s:動畫的總時間;
ease-out: 快結束的時候慢下來;
1s:停頓1秒后開始動畫;
backwards:動畫結束后回到原點
默認:播放一次
或者
transition:left 4s ease-out :hover{left:200px}
兼容主流瀏覽器:
.test{ -webkit-animation: < 各種屬性值 >; -moz-animation: < 各種屬性值 >; -o-animation: < 各種屬性值 >; animation: < 各種屬性值 >; }
animation-name,規定要綁定的keyframes
的名稱,隨便你取,不過為了日后維護的方便,建議取一個跟動作相關名稱相近的名稱比較好。比如要我們要綁定一個跑的動作,那么可以命名為run。
time,這里有兩個時間,前面一個是規定完成這個動畫所需要的時間,全稱叫animation-duration
,第二個time為動畫延遲開始播放的時間,全稱叫animation-delay
,這兩個數值可以用秒’s’也可以用微秒’ms’來寫,1000ms=1s,這個不用一一介紹。
animation-timing-function,規定動畫的運動曲線,這里有9個值,分別是ease
| linear
| ease-in
| ease-out
| ease-in-out
| step-start
| step-end
| steps
([, [ start
| end
] ]?) | cubic-bezier(x1, y1, x2, y2)
ease
:動畫緩慢開始,接着加速,最后減慢,默認值;linear
:動畫從頭到尾的速度是相同的;ease-in
:以低速開始;ease-out
:以低速結束;ease-in-out
:動畫以低速開始和結束;效果一樣 (按步數)steps
.test1{ background:url(http://img.xiaoho.com/2014/12/test.png) no-repeat 0 0; -webkit-animation:run 350ms steps(1) infinite 0s;} @-webkit-keyframes run { 0% { background-position:0; } 20% { background-position:-90px 0; } 40% { background-position:-180px 0; } 60% { background-position:-270px 0; } 80% { background-position:-360px 0; } 100% { background-position:-450px 0; } } .test2{ background:url(http://img.xiaoho.com/2014/12/test.png) no-repeat 0 0; -webkit-animation:run 350ms steps(5) infinite 0s;} @-webkit-keyframes run { 100% { background-position:-450px 0; } }animation-iteration-count,動畫播放次數,默認值為1,
infinite
為無限制,假如設置為無限制,那么動畫就會不停地播放。- animation-direction,規定動畫是否反方向運動。
=normal
|reverse
|alternate
|alternate-reverse
第一個值是正常轉動播放,默認值,reverse
為反向轉動,alternate一開始正常轉動,播放完一次之后接着再反向轉動,假如設置animation-iteration-count:1
則該值無效,alternate-reverse
一開始為反向轉動,播完一次之后按照回歸正常轉動,交替轉動,設置count
為1,則該值無效。 - animation-play-state,定義動畫是否運行或暫停,這是后來新增的屬性,有兩個屬性值分別是
running
和paused
。默認值為normal
,動畫正常播放。假如是為paused
,那么動畫暫停。假如一個動畫一開始為運動,那么假如設置paused
那么該動畫暫停,假如再設置running
,那么該動畫會從剛才暫停處開始運動 - animation-fill-mode,定義動畫播放時間之外的狀態,顧名思義,要么就是在動畫播放完了之后給它一個狀態
animation-fill-mode
:none
|forwards
|backwards
|both
;none
,播放完之后不改變默認行為,默認值,forwards
則是停在動畫最后的的那個畫面,backwards
則是回調到動畫最開始出現的畫面,both
則應用為動畫結束或開始的狀態