大网数据广告活动
Web 标准与前端开发

Element:animationend 事件

· 52CE 我爱测

当 CSS 动画完成时,会触发 **`animationend`** 事件。若动画在完成前中止(例如元素从 DOM 中移除,或动画从元素上移除),则不会触发 `animationend` 事件。

Web Animations

当 CSS 动画完成时,会触发 animationend 事件。若动画在完成前中止(例如元素从 DOM 中移除,或动画从元素上移除),则不会触发 animationend 事件。

语法

在诸如 EventTarget.addEventListener 等方法中使用事件名称,或设置事件处理器属性。

addEventListener("animationend", (event) => { })

onanimationend = (event) => { }

事件类型

AnimationEvent。继承自 Event。

AnimationEvent

示例

此示例获取一个正在播放动画的元素,并监听 animationend 事件:

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

animated.addEventListener("animationend", () => {
  console.log("动画已结束");
});

同上,但使用 onanimationend 事件处理器属性:

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

animated.onanimationend = () => {
  console.log("动画已结束");
};

运行实例

HTML

<div class="animation-example">
  <div class="container">
    <p class="animation">你选择了一个寒冷的夜晚到访我们的星球。</p>
  </div>
  <button class="activate" type="button">激活动画</button>
  <div class="event-log"></div>
</div>

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: slide-in;
  animation-iteration-count: 2;
}

@keyframes slide-in {
  from {
    transform: translateX(100%) scaleX(3);
  }

  to {
    transform: translateX(0) scaleX(1);
  }
}

JavaScript

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.addEventListener("animationiteration", () => {
  iterationCount++;
  animationEventLog.textContent = `${animationEventLog.textContent}'动画迭代次数:${iterationCount}' `;
});

animation.addEventListener("animationend", () => {
  animationEventLog.textContent = `${animationEventLog.textContent}'动画已结束'`;
  animation.classList.remove("active");
  applyAnimation.textContent = "激活动画";
});

animation.addEventListener("animationcancel", () => {
  animationEventLog.textContent = `${animationEventLog.textContent}'动画已取消'`;
});

applyAnimation.addEventListener("click", () => {
  animation.classList.toggle("active");
  animationEventLog.textContent = "";
  iterationCount = 0;
  const active = animation.classList.contains("active");
  applyAnimation.textContent = active ? "取消动画" : "激活动画";
});

结果

运行实例

规范

浏览器兼容性

参见

  • CSS 动画
  • 使用 CSS 动画
  • AnimationEvent
  • 相关事件:Element/animationstart_event、Element/animationcancel_event、Element/animationiteration_event

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。