Back to articles
CSS @starting-style: Animating Elements Into Existence

CSS @starting-style: Animating Elements Into Existence

via Dev.to WebdevPete Benoit

The enter animation problem has been with us since CSS transitions were invented. @starting-style finally gives us a clean, declarative solution. There's a class of bug that every CSS developer encounters eventually, usually while trying to animate a toast notification or a dialog. You add the element to the DOM, apply the transition, and… nothing. The element snaps in instantly, ignoring the transition entirely. The reason is simple once you understand it: CSS transitions animate between two computed states. When an element is first inserted, there is no previous state. The browser has nothing to interpolate from, so the transition is skipped. The old workarounds Most of us reached for the same duct-tape solutions. Force a reflow, then set the target state: // Option 1: setTimeout (fragile, tied to frame timing) toast . classList . remove ( ' visible ' ); setTimeout (() => toast . classList . add ( ' visible ' ), 16 ); // Option 2: requestAnimationFrame (better, still manual) requestA

Continue reading on Dev.to Webdev

Opens in a new tab

Read Full Article
2 views

Related Articles