La règle @keyframes
La règle @keyframes définit les images clés
de l'animation. Une image clé représente
les différentes propriétés de notre élément CSS, par exemple la position,
la taille, la couleur, etc., qui sont appliquées à l'élément à
un moment spécifié.
Syntaxe
@keyframes nom-de-l-animation {
images-clés
}
Exemple
Définissons les images clés :
<div id="elem"></div>
@keyframes anim {
from {
margin-left: 0px;
}
to {
margin-left: 400px;
}
}
#elem {
animation: anim 3s infinite;
border: 1px solid black;
width: 50px;
height: 50px;
}
:
Exemple
Maintenant, déplaçons notre carré du haut vers le bas à l'aide d'une animation :
<div id="elem"></div>
@keyframes anim {
from {
margin-top: 0%;
}
to {
margin-top: 10%;
}
}
#elem {
animation: anim 2s infinite;
border: 1px solid black;
width: 50px;
height: 50px;
}
:
Exemple
Augmentons la largeur de notre forme avec une animation, en définissant les points clés en pourcentages :
<div id="elem"></div>
@keyframes anim {
from {
width: 10%;
}
to {
width: 40%;
}
}
#elem {
animation: anim 2s infinite;
border: 1px solid black;
width: 50px;
height: 50px;
}
:
Exemple
Maintenant, changeons la transparence de notre forme :
<div id="elem"></div>
@keyframes anim {
from {
background-color: #467CBC;
}
to {
background-color: #C2DDFD;
}
}
#elem {
animation: anim 2s infinite;
border: 1px solid black;
width: 50px;
height: 50px;
}
:
Voir aussi
-
toutes les propriétés pour l'animation CSS
animation-name,animation-duration,animation-delay,animation-timing-function,animation-iteration-count,animation-direction,animation-fill-mode,animation-play-state,keyframes -
transition- transitions fluides (animation au survol de l'élément)