CSS3 animation(動畫) 屬性
定義和用法
animation 屬性是一個簡寫屬性,用於設定六個動畫屬性:
- animation-name
- animation-duration
- animation-timing-function
- animation-delay
- animation-iteration-count
- animation-direction
註釋:請始終規定 animation-duration 屬性,否則時長為 0,就不會播放動畫了。
語法
animation: name | duration | timing-function | delay | iteration-count | direction | fill-mode | play-state;
值 1 | 說明 |
---|---|
animation-name | 規定需要繫結到選擇器的keyframe名稱。 |
animation-duration | 規定動畫所花費的時間,以秒或毫秒計 |
animation-timing-funcion | 規定動畫時的速度曲線 |
animation-delay | 規定動畫開始前的延遲 |
animation-iteration-count | 規定動畫播放的次數 |
animation-direction | 規定是否應該輪流反向播放動畫 |
animation-fill-mode | 規定當動畫不播放時(當動畫完成時,或當動畫有一個延遲未開始播放時),要應用到元素的樣式。 |
animation-play-state | 指定動畫是否正在執行或已暫停。 |
初始值
- animation-name: none
- animation-duration: 0s
- animation-timing-function: ease
- animation-delay: 0s
- animation-iteration-count: 1
- animation-direction: normal
- animation-fill-mode: none
- animation-play-state: running
animation-name屬性
為 @keyframes 動畫指定一個名稱
語法
animation-name: keyframename|none;
為 @keyframes 動畫指定一個名稱::
animation-name:mymove;
-webkit-animation-name:mymove; /* Safari 和 Chrome */
animation-duration 屬性
定義動畫完成一個週期需要多少秒或毫秒。 語法 animation-duration: time;
div
{
animation:mymove infinite;
animation-duration:2s;
/* Safari and Chrome */
-webkit-animation:mymove infinite;
-webkit-animation-duration:2s;
}
@keyframes mymove
{
from {opacity:1;}
to {opacity:0;}
}
@-webkit-keyframes mymove /*Safari and Chrome*/
{
from {opacity:1;}
to {opacity:0;}
}
animation-timing-function 屬性
animation-timing-function指定動畫將如何完成一個週期。 速度曲線定義動畫從一套 CSS 樣式變為另一套所用的時間。 速度曲線用於使變化更為平滑。
語法
animation-timing-function: value;
值 | 描述 |
---|---|
linear | 動畫從頭到尾的速度是相同的。 |
ease | 預設。動畫以低速開始,然後加快,在結束前變慢。 |
ease-in | 動畫以低速開始。 |
ease-out | 動畫以低速結束。 |
ease-in-out | 動畫以低速開始和結束。 |
cubic-bezier(n,n,n,n) | 在 cubic-bezier 函式中自己的值。可能的值是從 0 到 1 的數值。 |
從開始到結束以相同的速度播放動畫:
animation-timing-function:linear;
-webkit-animation-timing-function:linear; /* Safari and Chrome */
animation-delay 屬性
animation-delay 屬性定義動畫什麼時候開始。 animation-delay 值單位可以是秒(s)或毫秒(ms)。 提示: 允許負值,-2s 使動畫馬上開始,但跳過 2 秒進入動畫。
等待兩秒,然後開始動畫:
animation-delay:2s;
-webkit-animation-delay:2s; /* Safari 和 Chrome */
animation-iteration-count 屬性 animation-iteration-count屬性定義動畫應該播放多少次。 語法 animation-iteration-count: value; value可取具體數值n,或者infinite(無限次)
播放三次動畫:
animation-iteration-count:3;
-webkit-animation-iteration-count:3; /*Safari and Chrome*/
animation-direction 屬性
animation-direction 屬性定義是否迴圈交替反向播放動畫。 注意:如果動畫被設定為只播放一次,該屬性將不起作用。 CSS 語法 animation-direction: normal|reverse|alternate|alternate-reverse|initial|inherit; 屬性值
值 | 描述 |
---|---|
normal | 預設值。動畫按正常播放。 |
reverse | 動畫反向播放。 |
alternate | 動畫在奇數次(1、3、5...)正向播放,在偶數次(2、4、6...)反向播放。 |
alternate-reverse | 動畫在奇數次(1、3、5...)反向播放,在偶數次(2、4、6...)正向播放。 |
initial | 設定該屬性為它的預設值。請參閱 initial。 |
inherit | 從父元素繼承該屬性。請參閱 inherit。 |
先執行一遍動畫,然後再反向執行一遍動畫:
animation-direction:alternate;
-webkit-animation-direction:alternate; /* Safari 和 Chrome */
animation-fill-mode 屬性
animation-fill-mode 屬性規定當動畫不播放時(當動畫完成時,或當動畫有一個延遲未開始播放時),要應用到元素的樣式。 預設情況下,CSS 動畫在第一個關鍵幀播放完之前不會影響元素,在最後一個關鍵幀完成後停止影響元素。animation-fill-mode 屬性可重寫該行為。
CSS 語法
animation-fill-mode: none|forwards|backwards|both|initial|inherit;
屬性值
值 | 描述 |
---|---|
none | 預設值。動畫在動畫執行之前和之後不會應用任何樣式到目標元素。 |
forwards | 在動畫結束後(由 animation-iteration-count |
backwards | 動畫將應用在 animation-delay 定義期間啟動動畫的第一次迭代的關鍵幀中定義的屬性值。這些都是 from 關鍵幀中的值(當 animation-direction 為 "normal" 或 "alternate" 時)或 to 關鍵幀中的值(當 animation-direction 為 "reverse" 或 "alternate-reverse" 時)。 |
both | 動畫遵循 forwards 和 backwards 的規則。也就是說,動畫會在兩個方向上擴充套件動畫屬性。 |
initial | 設定該屬性為它的預設值。 |
inherit | 從父元素繼承該屬性。 |
把物體動畫地從一個地方移動到另一個地方,並讓它停留在那裡:
animation-fill-mode:forwards;
-webkit-animation-fill-mode:forwards; /* Safari 和 Chrome */
語法
animation-play-state: paused|running; paused 指定暫停動畫 running 指定正在執行的動畫
animation-play-state 屬性
animation--play-state屬性指定動畫是否正在執行或已暫停。 注意:在JavaScript中使用此屬性在一個週期中暫停動畫。
暫停動畫:
animation-play-state:paused;
-webkit-animation-play-state:paused; /* Safari 和 Chrome */