Accessible web animations without a library

Build smooth web animations with CSS, IntersectionObserver, and requestAnimationFrame—without hiding content or ignoring reduced-motion preferences.

JavaScriptAccessibilityFrontend

Sources are linked in the guide. Read our editorial policy.

On this page 8 sections

A moving interface can help explain how a product works. It can connect sections, show that an action has finished, or give a portfolio a little personality. It can also get in the way: a long entrance animation delays reading, a scroll effect can make someone feel unwell, and an invisible heading is still invisible when the animation script fails.

The useful question is not “how much motion can this page have?” It is “what does this motion help someone understand?”

For many small sites, CSS and a few browser APIs are enough. The important part is making motion an enhancement rather than a requirement for using the page.

Start with a page that works without motion

Write the content and links first. Make sure a reader can reach every section without waiting for JavaScript, scrolling through a theatrical introduction, or interacting with a custom cursor.

A simple article or project card should be visible in the default HTML and CSS:

<article class="project-card">
  <h2>A useful developer tool</h2>
  <p>A short explanation of what it does and who it helps.</p>
  <a href="/projects/developer-tool/">Read about the project</a>
</article>

Do not begin with a blanket rule that sets every card to opacity: 0. That turns a decorative feature into a dependency. If a script never runs, those cards disappear for everyone—not just for people who turn JavaScript off.

This baseline also makes the content easier for search engines to discover. But crawlable markup is only one part of SEO; useful content, descriptive URLs, accurate metadata, and good performance still matter.

Treat reduced motion as a different experience

The browser exposes a preference for less motion through prefers-reduced-motion. It is not a request to make the exact same animation slightly faster. For a large zoom, parallax scene, or pinned horizontal story, the better alternative may be no animation at all.

Use the preference in both CSS and JavaScript:

.project-card {
  opacity: 1;
  transform: none;
}

@media (prefers-reduced-motion: no-preference) {
  .project-card.is-waiting {
    opacity: 0;
    transform: translateY(16px);
  }

  .project-card.is-revealing {
    transition: opacity 280ms ease, transform 280ms ease;
  }
}

@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }
}
const reducedMotion = window.matchMedia(
  '(prefers-reduced-motion: reduce)'
);

function updateMotionPreference() {
  if (reducedMotion.matches) {
    // Cancel active effects and restore the readable baseline.
  } else {
    // Enable the optional motion experience.
  }
}

reducedMotion.addEventListener('change', updateMotionPreference);
updateMotionPreference();

Listen for changes rather than checking the preference only once. Someone can change their operating-system setting while the page is open.

The reduced-motion version should still have the same content, navigation, and functionality. Removing animation should not remove information.

Reveal elements with IntersectionObserver

A short reveal can establish hierarchy when a section enters the viewport. IntersectionObserver is a better fit than repeatedly measuring every card inside a scroll listener.

The following example only hides cards after the observer exists. It also restores them immediately if a reader enables reduced motion:

const cards = [...document.querySelectorAll('.project-card')];
const preference = window.matchMedia(
  '(prefers-reduced-motion: reduce)'
);
let observer;

function reveal(card) {
  card.classList.remove('is-waiting');
}

function configureReveals() {
  observer?.disconnect();
  cards.forEach(card => {
    card.classList.remove('is-waiting', 'is-revealing');
  });

  if (preference.matches || !('IntersectionObserver' in window)) {
    return;
  }

  observer = new IntersectionObserver(entries => {
    for (const entry of entries) {
      if (!entry.isIntersecting) continue;
      const card = entry.target;
      card.classList.add('is-revealing');
      reveal(card);
      observer.unobserve(card);
    }
  }, { threshold: 0.12 });

  for (const card of cards) {
    // Do not hide an element the keyboard user is already using.
    if (card.contains(document.activeElement)) continue;
    card.classList.add('is-waiting');
    observer.observe(card);
  }
}

for (const card of cards) {
  card.addEventListener('focusin', () => {
    reveal(card);
    observer?.unobserve(card);
  });
}

preference.addEventListener('change', configureReveals);
configureReveals();

Pair this with the CSS above. Keep the displacement small and the duration short. A reveal should acknowledge that content arrived, not force the reader to watch it arrive.

Notice the focus handling. An element with zero opacity can still contain a focusable link. If a keyboard user tabs into a card before the observer reveals it, the card needs to become visible immediately.

Use requestAnimationFrame for scroll-linked updates

Some effects depend on continuous progress rather than a one-time reveal. A line that draws as someone scrolls is one example. For these, schedule visual writes with requestAnimationFrame.

A pending-frame guard prevents several scroll events from scheduling several redundant callbacks before the next frame:

const section = document.querySelector('.story');
const line = document.querySelector('.story-line');
let frame = 0;

function paintProgress() {
  frame = 0;
  const rect = section.getBoundingClientRect();
  const distance = Math.max(1, rect.height - window.innerHeight);
  const progress = Math.min(1, Math.max(0, -rect.top / distance));

  line.style.setProperty('--progress', String(progress));
}

function schedulePaint() {
  if (frame) return;
  frame = requestAnimationFrame(paintProgress);
}

window.addEventListener('scroll', schedulePaint, { passive: true });
window.addEventListener('resize', schedulePaint);
schedulePaint();

This is a scheduling pattern, not a complete reduced-motion implementation. Only attach these listeners when the effect is enabled. When disabling it, remove the listeners, cancel the pending frame, and reset any inline styles that would otherwise leave the content in a transformed state.

For most UI transitions, prefer transform and opacity over repeatedly changing layout properties such as width, height, or left. These properties often avoid layout work, but they are not a guarantee of free animation: large layers, filters, SVG drawing, and expensive paint operations still need profiling.

Keep layout reads together, then perform style writes. Alternating reads and writes for each element can force the browser to recalculate layout repeatedly.

Do not let scroll effects break navigation

Pinned stories and horizontal panels need more care than simple reveals. A fragment link such as #about should still take someone to the right content. A focused link should never remain in a panel that has been translated outside the viewport.

For a scroll-driven portfolio, check these paths explicitly:

  • Open the page directly at a fragment URL.
  • Navigate with Tab and Shift+Tab through every panel.
  • Resize from a desktop layout to a narrow screen.
  • Enable reduced motion while the story is halfway through.
  • Use a normal anchor link to leave the story and reach the work section.

On smaller screens, ordinary vertical document flow is often a better experience than keeping the desktop choreography. Preserve semantic reading order even when the visual layout changes.

If an introduction temporarily covers the page, either keep it purely decorative and non-blocking or make dismissal obvious and immediate. Avoid playing it again on every navigation.

Test the failure modes, not just the happy path

A smooth animation on a fast laptop is not enough evidence that the interface is usable. Start with a few cheap checks:

  1. Disable JavaScript. Headings, content, and important links should still be present.
  2. Enable reduced motion. Large scroll transformations, zoom effects, and smooth scrolling should disappear where appropriate.
  3. Navigate with the keyboard. Watch the focus indicator and verify that every focused element is on screen.
  4. Try a small viewport. Look for clipped headings, horizontal overflow, and oversized decorative elements.
  5. Profile on a slower device. Use the browser’s performance tools to look for long tasks, repeated layout, and expensive paint work.
  6. Change settings mid-session. Resize and toggle reduced motion without reloading.

For an observer-based reveal, an unsupported API should mean “show the content,” not “leave the content hidden.” For a continuous animation, cleanup should restore a usable layout, not merely stop the timer.

Keep the motion budget small

A library is useful when the animation system genuinely needs its capabilities. For a handful of reveals and one scroll-linked effect, native APIs keep the dependency surface small and the behavior close to the browser.

The trade-off is that accessibility and cleanup are your responsibility. No animation package can decide whether a page’s motion is helpful, whether the reduced-motion version is complete, or whether a keyboard user can follow the story.

A good default is simple: make the page useful first, add motion where it explains something, and make opting out feel like a first-class experience.

Further reading