Element : évènement animationiteration
Baseline
Large disponibilité
Cette fonctionnalité est bien établie et fonctionne sur de nombreux appareils et versions de navigateurs. Elle est disponible sur tous les navigateurs depuis décembre 2019.
L'évènement animationiteration est déclenché lorsqu'une itération d'une animation CSS se termine et qu'une autre commence. Cet évènement ne se produit pas en même temps que l'évènement animationend, et ne se produit donc pas pour les animations avec un animation-iteration-count de 1.
Syntaxe
Utilisez le nom de l'évènement dans des méthodes comme addEventListener(), ou définissez une propriété de gestionnaire d'évènements.
addEventListener("animationiteration", (event) => { })
onanimationiteration = (event) => { }
Type d'évènement
Un objet AnimationEvent. Hérite de l'objet Event.
Propriétés de l'évènement
Hérite également des propriétés de son parent Event.
AnimationEvent.animationNameLecture seule-
Une chaîne de caractères contenant la valeur de la
animation-namequi a généré l'animation. AnimationEvent.elapsedTimeLecture seule-
Un
floatindiquant le temps pendant lequel l'animation a été exécutée, en secondes, lorsque cet évènement a été déclenché, à l'exclusion du temps pendant lequel l'animation était en pause. Pour un évènementanimationstart,elapsedTimeest0.0sauf s'il y avait une valeur négative pouranimation-delay, auquel cas l'évènement est déclenché avecelapsedTimecontenant(-1 * delay). AnimationEvent.pseudoElementLecture seule-
Une chaîne de caractères, commençant par
'::', contenant le nom du pseudo-élément sur lequel l'animation s'exécute. Si l'animation ne s'exécute pas sur un pseudo-élément mais sur l'élément, une chaîne de caractères vide :''.
Exemples
Ce code utilise animationiteration pour suivre le nombre d'itérations qu'une animation a effectuées :
const anime = document.querySelector(".anime");
let compteIteration = 0;
anime.addEventListener("animationiteration", () => {
compteIteration++;
console.log(`Nombre d'itérations de l'animation : ${compteIteration}`);
});
Le même exemple, mais en utilisant la propriété de gestionnaire d'évènements onanimationiteration :
const anime = document.querySelector(".anime");
let compteIteration = 0;
anime.onanimationiteration = () => {
compteIteration++;
console.log(`Nombre d'itérations de l'animation : ${compteIteration}`);
};
Exemple interactif
HTML
<div class="exemple-animation">
<div class="conteneur">
<p class="animation">
Vous avez choisi une nuit froide pour visiter notre planète.
</p>
</div>
<button class="activer" type="button">Activer l'animation</button>
<div class="journal-event"></div>
</div>
CSS
.conteneur {
height: 3rem;
}
.journal-event {
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
const animation = document.querySelector("p.animation");
const journalEventAnimation = document.querySelector(
".exemple-animation>.journal-event",
);
const appliquerAnimation = document.querySelector(
".exemple-animation>button.activer",
);
let compteIteration = 0;
animation.addEventListener("animationstart", () => {
journalEventAnimation.textContent = `${journalEventAnimation.textContent}'animation démarrée' `;
});
animation.addEventListener("animationiteration", () => {
compteIteration++;
journalEventAnimation.textContent = `${journalEventAnimation.textContent}'itérations de l'animation : ${compteIteration}' `;
});
animation.addEventListener("animationend", () => {
journalEventAnimation.textContent = `${journalEventAnimation.textContent}'animation terminée'`;
animation.classList.remove("active");
appliquerAnimation.textContent = "Activer l'animation";
});
animation.addEventListener("animationcancel", () => {
journalEventAnimation.textContent = `${journalEventAnimation.textContent}'animation annulée'`;
});
appliquerAnimation.addEventListener("click", () => {
animation.classList.toggle("active");
journalEventAnimation.textContent = "";
compteIteration = 0;
const active = animation.classList.contains("active");
appliquerAnimation.textContent = active
? "Annuler l'animation"
: "Activer l'animation";
});
Résultat
Spécifications
| Spécification |
|---|
| CSS Animations Level 1> # eventdef-globaleventhandlers-animationiteration> |
Compatibilité des navigateurs
Voir aussi
- Les animations CSS
- Utiliser les animations CSS
- L'interface
AnimationEvent - Évènements associés :
animationstart,animationend,animationcancel