animation-direction

Baseline 已广泛支持

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

animation-direction CSS 属性设置动画是向前、向后播放,还是在向前和向后播放序列之间交替。

试一试

animation-direction: normal;
animation-direction: reverse;
animation-direction: alternate;
animation-direction: alternate-reverse;
<section class="flex-column" id="default-example">
  <div id="example-element"></div>
  <button id="play-pause">Play</button>
</section>
#example-element {
  animation-duration: 3s;
  animation-iteration-count: infinite;
  animation-name: slide;
  animation-play-state: paused;
  animation-timing-function: ease-in;
  background-color: #1766aa;
  border-radius: 50%;
  border: 5px solid #333333;
  color: white;
  height: 150px;
  margin: auto;
  margin-left: 0;
  width: 150px;
}

#example-element.running {
  animation-play-state: running;
}

#play-pause {
  font-size: 2rem;
}

@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 button = document.getElementById("play-pause");

button.addEventListener("click", () => {
  if (el.classList.contains("running")) {
    el.classList.remove("running");
    button.textContent = "Play";
  } else {
    el.classList.add("running");
    button.textContent = "Pause";
  }
});

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

语法

css
/* Single animation */
animation-direction: normal;
animation-direction: reverse;
animation-direction: alternate;
animation-direction: alternate-reverse;

/* Multiple animations */
animation-direction: normal, reverse;
animation-direction: alternate, reverse, normal;

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

normal

动画在每个周期都向前播放。换句话说,每次动画循环时,动画都会重置到起始状态并重新开始。这是默认值。

反向

动画在每个周期都向后播放。换句话说,每次动画循环时,动画都会重置到结束状态并重新开始。动画步骤反向执行,缓和函数也反转。例如,ease-in 缓和函数变为 ease-out

交替

动画在每个周期反转方向,第一次迭代向前播放。判断周期是偶数还是奇数的计数从一开始。

alternate-reverse

动画在每个周期反转方向,第一次迭代向后播放。判断周期是偶数还是奇数的计数从一开始。

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

注意:创建CSS 滚动驱动动画时,指定 animation-direction 按预期工作,例如 reverse 会导致动画在时间线推进过程中反向运行。alternate 值(结合 animation-iteration-count)会导致动画在时间线推进过程中向前和向后运行。

正式定义

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

正式语法

animation-direction = 
<single-animation-direction>#

<single-animation-direction> =
normal |
reverse |
alternate |
alternate-reverse

示例

反转动画方向

HTML

html
<div class="box"></div>

CSS

css
.box {
  background-color: rebeccapurple;
  border-radius: 10px;
  width: 100px;
  height: 100px;
}

.box:hover {
  animation-name: rotate;
  animation-duration: 0.7s;
  animation-direction: reverse;
}

@keyframes rotate {
  0% {
    transform: rotate(0);
  }
  100% {
    transform: rotate(360deg);
  }
}

结果

有关示例,请参阅 CSS 动画

规范

规范
CSS 动画级别 1
# animation-direction

浏览器兼容性

另见