← Tous les articles

JavaScript scroll-reveal animations with IntersectionObserver, no library needed

Fading sections in as visitors scroll needs neither jQuery nor an animation library. The code we ship, plus the mistakes that hurt accessibility and Core Web Vitals.

JavaScript scroll-reveal animations with IntersectionObserver, no library needed

Key points

  • IntersectionObserver tells you when an element enters the viewport, with no scroll listener.
  • Only hide content when JavaScript is running, and honour prefers-reduced-motion.
  • Never animate the main above-the-fold block: it delays LCP.

“Can the sections appear as you scroll down?” comes up on almost every brochure-site design we receive. Plenty of themes answer it with a full animation library, sometimes with jQuery in tow, for an effect that fits in a dozen lines. This is the version we build into our themes, along with the safeguards we learned the hard way.

Why not a scroll listener?

The old approach listened to scroll and called getBoundingClientRect() on every element dozens of times a second. You can feel it on a budget phone. IntersectionObserver hands that work to the browser, which only calls you back when an element crosses the threshold you set.

The complete code

Start with one line in the <head>, ideally before the stylesheets, to flag that JavaScript is available:

<script>document.documentElement.classList.add('js');</script>

The CSS hides elements only in that case, and switches the effect off for people who asked for reduced motion:

.js .reveal {
  opacity: 0;
  transform: translateY(1.5rem);
  transition: opacity .5s ease, transform .5s ease;
}
.js .reveal.is-visible {
  opacity: 1;
  transform: none;
}
@media (prefers-reduced-motion: reduce) {
  .js .reveal { opacity: 1; transform: none; transition: none; }
}

Then the script, loaded with defer:

const items = document.querySelectorAll('.reveal');

if ('IntersectionObserver' in window) {
  const observer = new IntersectionObserver((entries, obs) => {
    for (const entry of entries) {
      if (!entry.isIntersecting) continue;
      entry.target.classList.add('is-visible');
      obs.unobserve(entry.target); // animate once, then stop watching
    }
  }, { rootMargin: '0px 0px -10% 0px', threshold: 0.15 });

  items.forEach((el) => observer.observe(el));
} else {
  items.forEach((el) => el.classList.add('is-visible'));
}

The negative bottom rootMargin fires the animation slightly after the element enters the screen, so people actually see it happen. Calling unobserve() stops it replaying every time someone scrolls back up, which gets tiresome quickly.

Mistakes we fix most often

  • Hiding content unconditionally in CSS: if the script fails or gets blocked, the page stays blank. The js class on the root element prevents that.
  • Animating the hero: a main heading or image starting at opacity: 0 pushes back Largest Contentful Paint. Keep the effect below the fold.
  • A threshold that is too high: with threshold: 0.5, a block taller than two screens never reaches 50% visibility and never appears. Use a low threshold for tall sections.
  • Animating top or margin: that triggers layout work. transform and opacity stay smooth.

Staggering a grid

To make the cards in one row appear one after another, a delay based on the column is enough:

document.querySelectorAll('.cards .reveal').forEach((el, i) => {
  el.style.transitionDelay = `${(i % 3) * 80}ms`; // 3-column grid
});

Keep it subtle. Past a few hundred milliseconds, the effect makes a site feel slow rather than polished.

FAQ

Does this hurt SEO?

No. The text is still in the HTML and Google indexes it as usual. The real risk is to Core Web Vitals when above-the-fold content is animated.

How do I use it in WordPress?

Add the reveal class to the blocks you want through the editor’s “Additional CSS class(es)” field, and enqueue the script only on the pages that use it.

Want a site that feels alive without giving up speed? We build light, accessible animations into custom WordPress themes.

Partager LinkedIn Facebook X WhatsApp
</>

webtestudio

Agence web : sites WordPress sur mesure, thèmes premium et logiciels SaaS.

Parler de votre projet →