Performance: Keyframe animations
Keyframe animations affect the browser's paint and composite stages, impacting smoothness and visual stability during animations.
Jump into concepts and practice - no test required
@keyframes moveAndScale { 0% { transform: translateX(0) scaleX(1); } 100% { transform: translateX(200px) scaleX(3); } } .element { animation: moveAndScale 2s infinite; will-change: transform; position: absolute; }
@keyframes moveAndResize { 0% { left: 0; width: 100px; } 100% { left: 200px; width: 300px; } } .element { position: absolute; animation: moveAndResize 2s infinite; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Animating 'left' and 'width' | High (style changes affect layout) | Multiple per frame | High (full paint) | [X] Bad |
| Animating 'transform' and 'opacity' | Low (no layout changes) | None | Low (composite only) | [OK] Good |
@keyframes rule do in CSS animations?@keyframes@keyframes rule defines how styles change at different points during the animation timeline.animation-duration or animation-name apply the animation, but @keyframes sets the actual style changes.@keyframes defines animation steps [OK]@keyframes sets animation steps [OK]@keyframes with animation properties@keyframes applies animation to elementsfade that changes opacity from 0 to 1?@keyframes syntax@keyframes followed by the animation name and curly braces containing percentage or keyword steps.from and to or percentages like 0% and 100%. @keyframes fade { from { opacity: 0; } to { opacity: 1; } } uses from and to correctly.@keyframes syntax uses from and to [OK]@keyframes with from/to or 0%/100% [OK]@animation instead of @keyframesstart or end<div> element when the page loads?div {
width: 100px;
height: 100px;
background-color: red;
animation: slide 2s forwards;
}
@keyframes slide {
0% { transform: translateX(0); }
100% { transform: translateX(200px); }
}slide that lasts 2 seconds and uses forwards fill mode, meaning it keeps the final state.slide animation moves the div from translateX(0) to translateX(200px), which moves it 200 pixels to the right.@keyframes grow {
0% { width: 100px; }
100% { width: 200px }
}
.box {
animation-name: grow;
animation-duration: 3s;
animation-iteration-count: infinite
}@keyframeswidth: 200px line is missing a semicolon.animation-name, animation-duration, and animation-iteration-count are correct and properly used.width: 200px in keyframes -> Option A