CSS Animation Patterns

Search for a command to run...

No comments yet. Be the first to comment.
Bun, the hyper-fast JavaScript runtime known for its incredible speed and Zig-based architecture, is being completely rewritten in Rust. For a project that has over 22 million monthly downloads and ea

Next.js 16.3 is almost here, and it's packed with a ton of improvements and let's see them in this post 1.Instant Navigation For a while now, there’s been a valid, lingering debateServer Components vs

It’s official. React has merged its long-awaited Rust port of the React Compiler (formerly known as React Forget) into the main repository. What started as an experimental research project by Joseph S

If you follow modern technology trends, you have likely been led to believe that artificial intelligence is entirely built on Python. Every tutorial, machine learning framework documentation, and open

Just when the JavaScript ecosystem was catching its breath after last week’s TanStack Router compromise, the Mini Shai-Hulud supply chain worm has continued its relentless march. Developed by the thre

If you don’t have time to read but want to know what’s there in this post. Find the quick read 👇

Quick Read about Animation Patterns
In this post, we are going to look at the commonly used animation patterns. These patterns demonstrate how we can create engaging animations and interactions while still respecting users’ preferences for motion. They also support all modern browsers, so you can use them with confidence.
Animated Letters
Animated Words
This pattern shows how to create infinitely animated letter effects that respect the user’s motion preferences.
The goal is to animate progressively. The text should be readable by default, with the animation built on top. Split text motion effects can get extravagant and potentially disruptive, so we will only manipulate HTML, or apply motion styles if the user is OK with motion.
@media (prefers-reduced-motion: reduce)
Media query will be used from CSS and JavaScript in this project. This media query is our primary conditional for deciding whether to split text or not. The CSS media query will be used to withhold transitions and animations, while the JavaScript media query will be used to withhold the HTML manipulation.
PostCSS has been used to enable the syntax of Media Queries, where you can store a media query boolean into a variable
@custom-media --motionOK (prefers-reduced-motion: no-preference);
In JavaScript, the browser provides a way to check media queries, I used destructuring to extract and rename the boolean result from the media query check
const {matches:motionOK} = window.matchMedia(
'(prefers-reduced-motion: no-preference)'
)
I can then test for motionOK, and only change the document if the user has not requested to reduce motion.
if (motionOK) {
// document split manipulations
}

The user is OK with motion; text split into multiple elements

Stopping the motion in the render
This pattern shows how to create infinitely animated word effects that respect the user’s motion preferences.
Similar to splitting letters, this function takes a string and returns each word in an array.
export const byWord = text =>
text.split(' ').map(span)
The split() method on JavaScript strings allows us to specify which characters to slice at. I passed an empty space, indicating a split between words.
