--- title: Animation.playState slug: Web/API/Animation/playState tags: - Animation - animation api - 动画 - 运动状态 translation_of: Web/API/Animation/playState ---
{{APIRef("Web Animations")}}{{SeeCompatTable}}
作为一个 Web Animations API 的属性,Animation.playState 能够返回并设置一个可枚举值来描述一个动画的回放状态。
这个属性只对CSS Animations 和 Transitions可读。
var currentPlayState = Animation.playState; Animation.playState = newState;
idlependingrunningpausedfinished在Growing/Shrinking Alice Game这个例子中, 玩家们可以凭Alice crying into a pool of tears结束游戏。出于性能原因,游戏里,眼泪只当可见之时才能运动。因此,这些泪滴必须在下面的情况下刚好暂停运动:
// 创建泪珠动画
tears.forEach(function(el) {
el.animate(
tearsFalling,
{
delay: getRandomMsRange(-1000, 1000), // 获取每一滴随机泪珠
duration: getRandomMsRange(2000, 6000), // 获取每一滴随机泪珠
iterations: Infinity,
easing: "cubic-bezier(0.6, 0.04, 0.98, 0.335)"
});
el.playState = 'paused';
});
// 结尾需要现出时播放眼泪降落动画
tears.forEach(function(el) {
el.playState = 'playing';
});
// 暂停并重置正在哭泣时的泪滴动画
tears.forEach(function(el) {
el.playState = "paused";
el.currentTime = 0;
});
| 格式 | 状态 | 注解 |
|---|---|---|
| {{SpecName('Web Animations', '#play-state', 'playState')}} | {{Spec2("Web Animations")}} | Initial definition. |
playState