keyframe 用來描述影格/畫面的狀態,使用 @keyframes 這個關鍵字
/* animation-name 為自訂名稱 */
@keyframes animation-name {
  from {
    transform: translateY(20px);
  }
  to {
    transform: translateY(0px);
  }
}
/* from, to 也可以用 0%, 100% 表示 */
@keyframes animation-name {
    0% {
        transform: translateY(20px);
    }
    100% {
        transform: translateY(0px);
    }
}
使用 @keyframes 定義每一個影格的樣式後,可以使用 animation 相關屬性設定動畫效果
animation-duration:播放 @keyframes 的時間,預設是 0s,不得為負數animation-timing-function:速度函數,例如:linear、ease,也可以自訂 cubic-bezier() 貝茲曲線animation-iteration-count:@keyframes 總共要播放的次數,要循環播放可以使用 infinite
animation-direction:播放方向
animation-delay:animation 延遲多久播放animation-play-state:用來播放或暫停 animationanimation-fill-mode:animation-fill-modeanimation-fill-mode 用來控制在動畫開始前和結束後的顯示畫面,animation 預設只會在動畫執行過程中套用 style,一旦動畫結束就會回到初始狀態。
動畫可分成四個階段:
而 animation-fill-mode 有四個值:
forwards:動畫完成後停在最後一個影格(最後一個影格會取決於 animation-direction 和 animation-iteration-count)除了一個一個定義 animation-* 屬性,也可以使用 animation 屬性縮寫
依序為:
animation-name
animation-duration
animation-timing-function
animation-delay
animation-iteration-count
animation-direction
animation-fill-mode
animation-play-state
*其中 animation-name 為必填,其他屬性值為選填,都會先使用預設的初始值
MDN Web Docs - animation
The CSS animation-fill-mode Property, Explained