--- title: Animation.play() slug: Web/API/Animation/play translation_of: Web/API/Animation/play ---
Web Animations API的{{ domxref("Animation") }}接口中的play() 方法 可开始或恢复动画的播放. 如果动画结束,则调用play()重新启动动画,从头开始播放。
animation.play();
无.
{{jsxref("undefined")}}
在 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 | Status | Comment |
|---|---|---|
| {{SpecName('Web Animations', '#dom-animation-play', 'play()')}} | {{Spec2("Web Animations")}} |
{{Compat("api.Animation.play")}}