元素:animationiteration 事件
當一個 CSS 動畫 的一次迭代結束,另一次迭代開始時,將觸發 animationiteration 事件。此事件不會與 animationend 事件同時發生,因此對於 animation-iteration-count 值為一的動畫,此事件不會觸發。
語法
在諸如 addEventListener() 之類的方法中使用事件名稱,或設定事件處理程式屬性。
js
addEventListener("animationiteration", (event) => { })
onanimationiteration = (event) => { }
事件型別
一個 AnimationEvent。繼承自 Event。
事件屬性
也繼承自其父級 Event 的屬性.
AnimationEvent.animationName只讀-
一個字串,包含生成動畫的
animation-name的值。 AnimationEvent.elapsedTime只讀-
一個
float,表示在觸發此事件時,動畫已執行的時間(以秒為單位),不包括動畫暫停的任何時間。對於animationstart事件,elapsedTime為0.0,除非animation-delay的值為負,在這種情況下,事件將以elapsedTime為(-1 * delay)觸發。 AnimationEvent.pseudoElement只讀-
一個字串,以
'::'開頭,包含動畫執行的 偽元素的名稱。如果動畫不在偽元素上執行,而是在元素上執行,則為空字串:''。
示例
此程式碼使用 animationiteration 來跟蹤動畫已完成的迭代次數
js
const animated = document.querySelector(".animated");
let iterationCount = 0;
animated.addEventListener("animationiteration", () => {
iterationCount++;
console.log(`Animation iteration count: ${iterationCount}`);
});
使用 onanimationiteration 事件處理程式屬性,效果相同
js
const animated = document.querySelector(".animated");
let iterationCount = 0;
animated.onanimationiteration = () => {
iterationCount++;
console.log(`Animation iteration count: ${iterationCount}`);
};
即時示例
HTML
html
<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>
CSS
css
.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: slide-in;
animation-iteration-count: 2;
}
@keyframes slide-in {
from {
transform: translateX(100%) scaleX(3);
}
to {
transform: translateX(0) scaleX(1);
}
}
JavaScript
js
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;
const active = animation.classList.contains("active");
applyAnimation.textContent = active
? "Cancel animation"
: "Activate animation";
});
結果
規範
| 規範 |
|---|
| CSS 動畫級別 1 # eventdef-globaleventhandlers-animationiteration |
瀏覽器相容性
載入中…