animation-fill-mode

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 2015 年 9 月以来,该特性已在各大浏览器中可用。

animation-fill-mode CSS 属性设置 CSS 动画在执行之前和之后如何将样式应用于其目标。

试一试

animation-fill-mode: none;
animation-delay: 1s;
animation-fill-mode: forwards;
animation-delay: 1s;
animation-fill-mode: backwards;
animation-delay: 1s;
animation-fill-mode: both;
animation-delay: 1s;
<section class="flex-column" id="default-example">
  <div>Animation <span id="play-status"></span></div>
  <div id="example-element">Select a mode to start!</div>
</section>
#example-element {
  background-color: #1766aa;
  color: white;
  margin: auto;
  margin-left: 0;
  border: 5px solid #333333;
  width: 150px;
  height: 150px;
  border-radius: 50%;

  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

#play-status {
  font-weight: bold;
}

.animating {
  animation: slide 1s ease-in 1;
}

@keyframes slide {
  from {
    background-color: orange;
    color: black;
    margin-left: 0;
  }
  to {
    background-color: orange;
    color: black;
    margin-left: 80%;
  }
}
const el = document.getElementById("example-element");
const status = document.getElementById("play-status");

function update() {
  status.textContent = "delaying";
  el.className = "";
  window.requestAnimationFrame(() => {
    window.requestAnimationFrame(() => {
      el.className = "animating";
    });
  });
}

el.addEventListener("animationstart", () => {
  status.textContent = "playing";
});

el.addEventListener("animationend", () => {
  status.textContent = "finished";
});

const observer = new MutationObserver(() => {
  update();
});

observer.observe(el, {
  attributes: true,
  attributeFilter: ["style"],
});

update();

通常,为了图方便,可以使用简写属性 animation 一次性设置所有动画属性。

语法

css
/* Single animation */
animation-fill-mode: none;
animation-fill-mode: forwards;
animation-fill-mode: backwards;
animation-fill-mode: both;

/* Multiple animations */
animation-fill-mode: none, backwards;
animation-fill-mode: both, forwards, none;

/* Global values */
animation-fill-mode: inherit;
animation-fill-mode: initial;
animation-fill-mode: revert;
animation-fill-mode: revert-layer;
animation-fill-mode: unset;

none

动画在未执行时不会对目标应用任何样式。元素将根据应用于它的其他 CSS 规则进行显示。这是默认值。

forwards

目标将保留执行期间遇到的最后一个 关键帧 所设置的计算值。最后一个关键帧取决于 animation-directionanimation-iteration-count 的值。

animation-direction animation-iteration-count 遇到的最后一个关键帧
normal 偶数或奇数 100%to
反向 偶数或奇数 0%from
交替 even 0%from
交替 odd 100%to
交替反向 even 100%to
交替反向 odd 0%from

动画属性的行为如同包含在一组 will-change 属性值中。如果动画期间创建了新的堆叠上下文,目标元素在动画结束后会保留该堆叠上下文。

backwards

动画将根据第一个相关 关键帧 中定义的值进行应用,并在 animation-delay 期间保持这些值。第一个相关关键帧取决于 animation-direction 的值。

animation-direction 第一个相关关键帧
normalalternate 0%from
reversealternate-reverse 100%to
both

动画将同时遵循 forwards 和 backwards 的规则,从而在两个方向上扩展动画属性。

备注: 当你在一个 animation-* 属性上指定多个逗号分隔的值时,它们会按照 animation-name 出现的顺序应用于动画。对于动画数量和 animation-* 属性值不匹配的情况,请参见设置多个动画属性值

注意: animation-fill-mode 在创建 CSS 滚动驱动动画 时与常规基于时间的动画具有相同的效果。

正式定义

初始值none
应用于所有元素,::before::after 伪元素
继承性
计算值同指定值
动画类型不可动画化

正式语法

animation-fill-mode = 
<single-animation-fill-mode>#

<single-animation-fill-mode> =
none |
forwards |
backwards |
both

示例

设置填充模式

您可以在以下示例中看到 animation-fill-mode 的效果。它演示了如何使动画保持在最终状态,而不是恢复到原始状态(这是默认行为)。

HTML

html
<p>Move your mouse over the gray box!</p>
<div class="demo">
  <div class="grows-and-stays">This grows and stays big.</div>
  <div class="grows">This just grows.</div>
</div>

CSS

css
.demo {
  border-top: 100px solid #cccccc;
  height: 300px;
}

@keyframes grow {
  0% {
    font-size: 0;
  }
  100% {
    font-size: 40px;
  }
}

.demo:hover .grows {
  animation-name: grow;
  animation-duration: 3s;
}

.demo:hover .grows-and-stays {
  animation-name: grow;
  animation-duration: 3s;
  animation-fill-mode: forwards;
}

结果

有关更多示例,请参阅 CSS 动画

规范

规范
CSS 动画级别 1
# animation-fill-mode

浏览器兼容性

另见