Window: cancelAnimationFrame() 方法

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

window.cancelAnimationFrame() 方法会取消通过调用 window.requestAnimationFrame() 方法之前安排好的动画帧请求。

语法

js
cancelAnimationFrame(requestID)

参数

requestID

调用 window.requestAnimationFrame() 请求回调时返回的 ID 值。

返回值

无(undefined)。

示例

js
const start = document.timeline.currentTime;

let myReq;

function step(timestamp) {
  const progress = timestamp - start;
  d.style.left = `${Math.min(progress / 10, 200)}px`;
  if (progress < 2000) {
    // it's important to update the requestId each time you're calling requestAnimationFrame
    myReq = requestAnimationFrame(step);
  }
}
myReq = requestAnimationFrame(step);
// the cancellation uses the last requestId
cancelAnimationFrame(myReq);

规范

规范
HTML
# animationframeprovider-cancelanimationframe

浏览器兼容性

另见