Performance: Animation mixin patterns
Animation mixin patterns affect the browser's paint and composite stages, impacting smoothness and CPU/GPU usage during animations.
Jump into concepts and practice - no test required
@mixin animate-opacity { animation: fadeInOpacity 1s ease-in-out; } .element { @include animate-opacity; will-change: opacity; }
@mixin animate-all { animation: fadeIn 1s ease-in-out; } .element { @include animate-all; transition: all 0.3s ease; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Animating 'all' properties via mixin | Multiple style recalculations | Multiple reflows per frame | High paint cost | [X] Bad |
| Animating 'opacity' only with will-change | Minimal style recalculations | No reflows | Low paint cost, GPU compositing | [OK] Good |
@mixin slide($distance, $duration) {
animation: slideIn $duration ease forwards;
transform: translateX($distance);
}
.box {
@include slide(100px, 2s);
}.box class?@mixin bounce($time) {
animation-name: bounce;
animation-duration $time;
animation-iteration-count: infinite;
}