元素:animationiteration 事件

animationiteration 事件在 CSS 动画 的一次迭代结束,而另一次迭代开始时触发。此事件不会与 animationend 事件同时发生,因此对于 animation-iteration-count 为 1 的动画不会发生。

语法

addEventListener() 等方法中使用事件名称,或设置事件处理程序属性。

js
addEventListener("animationiteration", (event) => {});

onanimationiteration = (event) => {};

事件类型

事件属性

还继承了其父类 Event 的属性.

AnimationEvent.animationName 只读

包含生成动画的 animation-name 值的字符串。

AnimationEvent.elapsedTime 只读

一个 float,表示此事件触发时动画已运行的时间量(以秒为单位),不包括动画暂停的任何时间。对于 animationstart 事件,elapsedTime0.0,除非 animation-delay 的值为负数,在这种情况下,事件将以包含 (-1 * delay)elapsedTime 触发。

AnimationEvent.pseudoElement 只读

一个以 '::' 开头的字符串,包含动画运行的 伪元素 的名称。如果动画不在伪元素上运行,而是在元素上运行,则为空字符串:''

示例

此代码使用 animationiteration 来跟踪动画完成的迭代次数

js
const animated = document.querySelector(".animated");

let iterationCount = 0;

animated.addEventListener("animationiteration", () => {
  iterationCount++;
  console.log(`Animation iteration count: ${iterationCount}`);
});

相同,但使用 onanimationiteration 事件处理程序属性

js
const animated = document.querySelector(".animated");

let iterationCount = 0;

animated.onanimationiteration = () => {
  iterationCount++;
  console.log(`Animation iteration count: ${iterationCount}`);
};

实时示例

HTML

html
<div class="animation-example">
  <div class="container">
    <p class="animation">You chose a cold night to visit our planet.</p>
  </div>
  <button class="activate" type="button">Activate animation</button>
  <div class="event-log"></div>
</div>

CSS

css
.container {
  height: 3rem;
}

.event-log {
  width: 25rem;
  height: 2rem;
  border: 1px solid black;
  margin: 0.2rem;
  padding: 0.2rem;
}

.animation.active {
  animation-duration: 2s;
  animation-name: slidein;
  animation-iteration-count: 2;
}

@keyframes slidein {
  from {
    transform: translateX(100%) scaleX(3);
  }
  to {
    transform: translateX(0) scaleX(1);
  }
}

JavaScript

js
const animation = document.querySelector("p.animation");
const animationEventLog = document.querySelector(
  ".animation-example>.event-log",
);
const applyAnimation = document.querySelector(
  ".animation-example>button.activate",
);
let iterationCount = 0;

animation.addEventListener("animationstart", () => {
  animationEventLog.textContent = `${animationEventLog.textContent}'animation started' `;
});

animation.addEventListener("animationiteration", () => {
  iterationCount++;
  animationEventLog.textContent = `${animationEventLog.textContent}'animation iterations: ${iterationCount}' `;
});

animation.addEventListener("animationend", () => {
  animationEventLog.textContent = `${animationEventLog.textContent}'animation ended'`;
  animation.classList.remove("active");
  applyAnimation.textContent = "Activate animation";
});

animation.addEventListener("animationcancel", () => {
  animationEventLog.textContent = `${animationEventLog.textContent}'animation canceled'`;
});

applyAnimation.addEventListener("click", () => {
  animation.classList.toggle("active");
  animationEventLog.textContent = "";
  iterationCount = 0;
  const active = animation.classList.contains("active");
  applyAnimation.textContent = active
    ? "Cancel animation"
    : "Activate animation";
});

结果

规范

规范
CSS 动画级别 1
# eventdef-globaleventhandlers-animationiteration

浏览器兼容性

BCD 表格仅在浏览器中加载

另请参阅