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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
---
title: 'HTMLElement: animationiteration event'
slug: Web/API/HTMLElement/animationiteration_event
tags:
- Animation
- CSS Animations
- animationiteration event
translation_of: Web/API/HTMLElement/animationiteration_event
---
<div>{{APIRef}}</div>
<p><strong><code>animationiteration</code></strong> 事件将被触发 当<a href="/en-US/docs/Web/CSS/CSS_Animations">CSS 动画</a>的迭代结束且另一个迭代开始时。此事件不会与 {{domxref("HTMLElement/animationend_event", "animationend")}} 事件同时发生t, 因此对于<code>animation-iteration-count</code>次数为1的动画不会发生。</p>
<table class="properties">
<tbody>
<tr>
<th>Bubbles</th>
<td>Yes</td>
</tr>
<tr>
<th>Cancelable</th>
<td>No</td>
</tr>
<tr>
<th>Interface</th>
<td>{{domxref("AnimationEvent")}}</td>
</tr>
<tr>
<th>Event handler property</th>
<td>{{domxref("GlobalEventHandlers/onanimationiteration","onanimationiteration")}}</td>
</tr>
</tbody>
</table>
<h2 id="例子">例子</h2>
<p>此代码使用 <code>animationiteration</code> 来跟踪动画已完成的迭代次数:</p>
<pre class="brush: js notranslate">const animated = document.querySelector('.animated');
let iterationCount = 0;
animated.addEventListener('animationiteration', () => {
iterationCount++;
console.log(`Animation iteration count: ${iterationCount}`);
});</pre>
<p>相同, 但使用 <code>onanimationiteration</code> 事件处理程序属性:</p>
<pre class="brush: js notranslate">const animated = document.querySelector('.animated');
let iterationCount = 0;
animated.onanimationiteration = () => {
iterationCount++;
console.log(`Animation iteration count: ${iterationCount}`);
};</pre>
<h3 id="鲜活的范例">鲜活的范例</h3>
<h4 id="HTML">HTML</h4>
<pre class="brush: html notranslate"><div class="animation-example">
<div class="container">
<p class="animation">You chose a cold night to visit our planet.</p>
</div>
<button class="activate" type="button">Activate animation</button>
<div class="event-log"></div>
</div>
</pre>
<h4 id="CSS">CSS</h4>
<pre class="brush: css notranslate">.container {
height: 3rem;
}
.event-log {
width: 25rem;
height: 2rem;
border: 1px solid black;
margin: 0.2rem;
padding: 0.2rem;
}
.animation.active {
animation-duration: 2s;
animation-name: slidein;
animation-iteration-count: 2;
}
@keyframes slidein {
from {
transform: translateX(100%) scaleX(3);
}
to {
transform: translateX(0) scaleX(1);
}
}
</pre>
<h4 id="JS">JS</h4>
<pre class="brush: js notranslate">const animation = document.querySelector('p.animation');
const animationEventLog = document.querySelector('.animation-example>.event-log');
const applyAnimation = document.querySelector('.animation-example>button.activate');
let iterationCount = 0;
animation.addEventListener('animationstart', () => {
animationEventLog.textContent = `${animationEventLog.textContent}'animation started' `;
});
animation.addEventListener('animationiteration', () => {
iterationCount++;
animationEventLog.textContent = `${animationEventLog.textContent}'animation iterations: ${iterationCount}' `;
});
animation.addEventListener('animationend', () => {
animationEventLog.textContent = `${animationEventLog.textContent}'animation ended'`;
animation.classList.remove('active');
applyAnimation.textContent = "Activate animation";
});
animation.addEventListener('animationcancel', () => {
animationEventLog.textContent = `${animationEventLog.textContent}'animation canceled'`;
});
applyAnimation.addEventListener('click', () => {
animation.classList.toggle('active');
animationEventLog.textContent = '';
iterationCount = 0;
let active = animation.classList.contains('active');
if (active) {
applyAnimation.textContent = "Cancel animation";
} else {
applyAnimation.textContent = "Activate animation";
}
});
</pre>
<h4 id="结果">结果</h4>
<iframe frameborder="no" height="265" src="" title="Live example"></iframe>
<h2 id="规范">规范</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName("CSS3 Animations", "#eventdef-animationevent-animationiteration")}}</td>
<td>{{Spec2("CSS3 Animations")}}</td>
<td>Initial definition</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>{{Compat("api.HTMLElement.animationiteration_event")}}</p>
<h2 id="了解更多">了解更多</h2>
<ul>
<li><a href="/en-US/docs/Web/CSS/CSS_Animations">CSS Animations</a></li>
<li><a href="/en-US/docs/Web/CSS/CSS_Animations/Using_CSS_animations">Using CSS Animations</a></li>
<li>{{domxref("AnimationEvent")}}</li>
<li>Related events: {{domxref("HTMLElement/animationstart_event", "animationstart")}}, {{domxref("HTMLElement/animationend_event", "animationend")}}, {{domxref("HTMLElement/animationcancel_event", "animationcancel")}}</li>
<li><code>This event on {{domxref("Document")}} targets: {{domxref("Document/animationiteration_event", "animationiteration")}}</code></li>
<li><code>This event on {{domxref("Window")}} targets: {{domxref("Window/animationiteration_event", "animationiteration")}}</code></li>
</ul>
|