Animation.play()

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

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

语法

animation.play(); 

参数

无.

返回值

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
Web Animations
play()
Working Draft

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
play
Experimental
Chrome Full support 39Edge Full support ≤79Firefox Full support 48
Full support 48
No support 46 — 48
Disabled
Disabled From version 46 until version 48 (exclusive): this feature is behind the dom.animations-api.core.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 26Safari No support NoWebView Android Full support 39Chrome Android Full support 39Firefox Android Full support 48
Full support 48
No support 46 — 48
Disabled
Disabled From version 46 until version 48 (exclusive): this feature is behind the dom.animations-api.core.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 26Safari iOS No support NoSamsung Internet Android Full support 4.0

Legend

Full support  
Full support
No support  
No support
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
User must explicitly enable this feature.
User must explicitly enable this feature.

了解更多