1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
---
title: Animation.play()
slug: Web/API/Animation/play
translation_of: Web/API/Animation/play
---
<div>{{ APIRef("Web Animations") }}{{SeeCompatTable}}</div>
<p><a href="/en-US/docs/Web/API/Web_Animations_API">Web Animations API</a>的{{ domxref("Animation") }}接口中的<strong><code>play()</code></strong> 方法 可开始或恢复动画的播放. 如果动画结束,则调用<code>play()</code>重新启动动画,从头开始播放。</p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox">animation.play();
</pre>
<h3 id="参数">参数</h3>
<p>无.</p>
<h3 id="返回值">返回值</h3>
<p>{{jsxref("undefined")}}</p>
<h2 id="例子">例子</h2>
<p>在 <a href="http://codepen.io/rachelnabors/pen/PNYGZQ?editors=0010">Growing/Shrinking Alice Game</a> 示例中, 单击或点击蛋糕会导致Alice的增长动画 (<code>aliceChange</code>) 播放,导致她体型变大并触发蛋糕的动画。在以下示例中,使用了一个事件监听器来触发两者的动画:</p>
<pre class="brush: 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);
</pre>
<h2 id="标准">标准</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('Web Animations', '#dom-animation-play', 'play()')}}</td>
<td>{{Spec2("Web Animations")}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div>
<p>{{Compat("api.Animation.play")}}</p>
</div>
<h2 id="了解更多" style="line-height: 30px; font-size: 2.14285714285714rem;">了解更多</h2>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API">Web Animations API</a></li>
<li>{{domxref("Animation")}} for other methods and properties you can use to control web page animation.</li>
<li>{{domxref("Animation.pause()")}} to pause an animation.</li>
<li>{{domxref("Animation.reverse()")}} to play an animation backwards.</li>
<li>{{domxref("Animation.finish()")}} to finish an animation.</li>
<li>{{domxref("Animation.cancel()")}} to cancel an animation.</li>
</ul>
|