Performance: Animation duration and delay
Animation duration and delay affect how long animations run and when they start, impacting user perception and browser rendering workload.
Jump into concepts and practice - no test required
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } } .element { animation-name: fadeIn; animation-duration: 0.5s; animation-delay: 0s; animation-fill-mode: forwards; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } } .element { animation-name: fadeIn; animation-duration: 5s; animation-delay: 3s; animation-fill-mode: forwards; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Long duration with delay | Minimal | 0 during delay, 1+ during animation frames | High due to continuous repaint | [X] Bad |
| Short duration without delay | Minimal | 0 during delay, fewer during animation frames | Low due to quick repaint | [OK] Good |
animation-duration control?animation-duration sets the length of time an animation takes to complete one cycle.animation-delay controls start time, not duration. Color and size are unrelated.: to assign values, not equals =.s for seconds without quotes.div {
animation-name: slide;
animation-duration: 3s;
animation-delay: 1s;
animation-fill-mode: forwards;
}
@keyframes slide {
from { transform: translateX(0); }
to { transform: translateX(100px); }
}animation-delay: 1s;.animation-duration: 3s;..box {
animation-duration: 2s;
animation-delay: 500ms;
animation-name: fadeIn;
animation-delay: 1s;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}animation-delay lines; the second one (1s) overrides the first (500ms).animation-delay: 2s; is correct.animation-duration: 4s; is correct.