這個屬性管的是「動畫之外」的兩個時刻:動畫開始前的那一瞬間,以及動畫結束後元素該停在什麼狀態。
語法:
animation-fill-mode:取值;
| 屬性值 | 說明 |
|---|---|
| none | 動畫播完最後一幀後回到初始狀態(預設值) |
| forwards | 動畫結束後繼續套用最後一個關鍵幀的樣式 |
| backwards | 在動畫正式開始前,先套用第一個關鍵幀的樣式 |
| both | 同時具備 forwards 與 backwards 的效果 |
舉例:
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>CSS3 animation-fill-mode屬性</title>
<style type="text/css">
@-webkit-keyframes mytranslate {
0%{}
100%{-webkit-transform:translateX(100px);}
}
div:not(#container) {
width:40px;
height:40px;
border-radius:20px;
background-color:red;
-webkit-animation-name:mytranslate;
-webkit-animation-timing-function:linear;
-webkit-animation-duration:2s;
}
#div2 { -webkit-animation-fill-mode:forwards; }
#div3 { -webkit-animation-fill-mode:backwards; }
#div4 { -webkit-animation-fill-mode:both; }
#container {
display:inline-block;
width:140px;
border:1px solid silver;
}
</style>
</head>
<body>
<div id="container">
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<div id="div4"></div>
</div>
</body>
</html>
分析:
實務上最常用的是 forwards——動畫跑完停在終點,不要「啪」地彈回原位,這在多數互動效果裡都是必要的。backwards 則常用在動畫帶有 delay 的場合,讓元素在等待期間就先保持起始狀態,避免出現位置跳動。