📚 Animations CSS
Les animations CSS permettent de créer des mouvements et des effets qui se déclenchent automatiquement, sans interaction de l'utilisateur. Contrairement aux transitions qui nécessitent un déclencheur (comme :hover
), les animations démarrent toutes seules.
Syntaxe de base
Créer une animation avec @keyframes
Pour créer une animation, on définit d'abord les étapes de l'animation avec @keyframes
.
@keyframes nom-animation {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
Appliquer l'animation
Ensuite, on applique l'animation à un élément avec la propriété animation
.
.element {
animation: nom-animation 2s;
}
info
La syntaxe de base est: animation: nom durée;
Étapes avec pourcentages
On peut définir plusieurs étapes avec des pourcentages pour des animations plus complexes.
@keyframes couleur-changeante {
0% {
background-color: #3498db;
}
50% {
background-color: #e74c3c;
}
100% {
background-color: #2ecc71;
}
}
Répéter l'animation
Pour répéter une animation, ajoutez le nombre de répétitions après la durée.
.element {
animation: mon-animation 2s 3; /* 3 fois */
animation: mon-animation 2s infinite; /* Boucle infinie */
}
Exemples d'animations courantes
Rotation
@keyframes rotation {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
.element {
animation: rotation 2s linear infinite;
}
Pulsation
@keyframes pulsation {
0%, 100% {
transform: scale(1);
}
50% {
transform: scale(1.2);
}
}
.element {
animation: pulsation 1s ease-in-out infinite;
}
Glissement
@keyframes glisser {
from {
transform: translateX(0);
}
to {
transform: translateX(100px);
}
}
.element {
animation: glisser 2s ease-in-out;
}