Animation.play()

Baseline 2022

Newly available

Since September 2022, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

实验性: 这是一项实验性技术
在将其用于生产之前,请仔细检查浏览器兼容性表格

Web Animations APIAnimation接口中的**play()** 方法 可开始或恢复动画的播放。如果动画结束,则调用play()重新启动动画,从头开始播放。

语法

animation.play();

参数

无。

返回值

例子

Growing/Shrinking Alice Game 示例中,单击或点击蛋糕会导致 Alice 的增长动画 (aliceChange) 播放,导致她体型变大并触发蛋糕的动画。在以下示例中,使用了一个事件监听器来触发两者的动画:

js
// 蛋糕拥有其自己的动画:
var nommingCake = document
  .getElementById("eat-me_sprite")
  .animate(
    [{ transform: "translateY(0)" }, { transform: "translateY(-80%)" }],
    {
      fill: "forwards",
      easing: "steps(4, end)",
      duration: aliceChange.effect.timing.duration / 2,
    },
  );

// 暂停蛋糕的动画,以避免动画立即播放。
nommingCake.pause();

// 该函数会在用户点击时触发
var growAlice = function () {
  // Play Alice's animation.
  aliceChange.play();

  // Play the cake's animation.
  nommingCake.play();
};

// 当用户持续按下或点击时,调用 growAlice 函数使得所有的动画播放。
cake.addEventListener("mousedown", growAlice, false);
cake.addEventListener("touchstart", growAlice, false);

标准

Specification
Web Animations
# dom-animation-play

浏览器兼容性

BCD tables only load in the browser

了解更多