Animation.play()

Experimental: 这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。

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

语法

animation.play();

参数

无。

返回值

例子

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

// 蛋糕拥有其自己的动画:
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

了解更多