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

Animation: remove event

· 52CE 我爱测

The **`remove`** event of the Animation interface fires when the animation is automatically removed by the browser.

Web Animations

The remove event of the Animation interface fires when the animation is automatically removed by the browser.

Syntax

Use the event name in EventTarget/addEventListener:

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

Alternatively, assign to the event handler property of the interface:

onremove = (event) => { }

Event type

An AnimationPlaybackEvent. Inherits from Event.

AnimationPlaybackEvent

Examples

Removing replaced animations

In this example we have a <button id="start"> element, and an event listener that runs whenever the mouse moves. The Element.mousemove_event event handler sets up an animation that animates the <button> to the position of the mouse pointer. This could result in a huge animations list, which could create a memory leak. For this reason, modern browsers automatically remove forwards-filling animations that are overridden by other animations.

The number of animations created is displayed. A remove event listener is used to count and display the number of animations removed as well.

All but one of the animations should eventually be removed.

HTML

<button id="start">Click to drag</button>
<br />
<button id="reset">Reset example</button>
<p>
  Click the button to start the animation (disabled by default to protect those
  who suffer migraines when experiencing such animations).
</p>
<p>Animations created: <span id="count-created">0</span></p>
<p>Animations removed: <span id="count-removed">0</span></p>

CSS

:root,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}

button {
  margin: 0.5rem 0;
}

JavaScript

const button = document.querySelector("#start");
let created = 0;
let removed = 0;

button.addEventListener(
  "click",
  () => {
    document.body.addEventListener("mousemove", (event) => {
      const animation = button.animate(
        { transform: `translate(${event.clientX}px, ${event.clientY}px)` },
        { duration: 500, fill: "forwards" },
      );
      created++;
      showCounts();

      // the remove event fires after the animation is removed
      animation.addEventListener("remove", () => {
        removed++;
        showCounts();
      });
    });
  },
  { once: true },
);

function showCounts() {
  document.getElementById("count-created").textContent = created;
  document.getElementById("count-removed").textContent = removed;
}

const reset = document.querySelector("#reset");
reset.addEventListener("click", () => {
  document.location.reload();
});

Result

Removing_replaced_animations

Specifications

Browser compatibility

See also

  • Web Animations API
  • Animation, AnimationPlaybackEvent
  • Animation.replaceState, to check whether an animation has been removed
  • Animation.persist(), to prevent removal of an animation

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

资料来源与适用范围

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

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

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