Animation.play()
Web Animations API的Animation
接口中的**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
了解更多
- Web Animations API
Animation
for other methods and properties you can use to control web page animation.Animation.pause()
(en-US) to pause an animation.Animation.reverse()
(en-US) to play an animation backwards.Animation.finish()
(en-US) to finish an animation.Animation.cancel()
to cancel an animation.