Back to library
INTERACTION_137

Scroll Triggered Text Changes.

A fixed caption swaps as the reader moves between blocks. The first caption visible before the trigger; cross-fading the caption with a 6px lift once each content block crossing the reading line.

CATEGORY
Scroll
COMPLEXITY
Easy
TECH
CSS
PLATFORM
Both
PERFORMANCE
Light
LIVE PLAYGROUND
CSS / CSS
/* Scroll Triggered Text Changes — A fixed caption swaps as the reader moves between blocks. */
.scroll-triggered-text-changes {
  --dur: 360ms;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
  --stagger: 60ms;
  will-change: transform, opacity;
}

.scroll-triggered-text-changes > * {
  opacity: 0;
  transform: translateY(14px);
  animation: scroll-triggered-text-changes-in var(--dur) var(--ease) forwards;
  animation-delay: calc(var(--i, 0) * var(--stagger));
}

/* the first caption visible -> cross-fading the caption with a 6px lift */
@keyframes scroll-triggered-text-changes-in {
  to {
    opacity: 1;
    transform: none;
    filter: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .scroll-triggered-text-changes > * {
    opacity: 1;
    transform: none;
    animation: none;
  }
}
React / TSX
import { useEffect, useRef } from "react";

/** Scroll Triggered Text Changes — trigger: each content block crossing the reading line */
export function ScrollTriggeredTextChanges({ children }: { children: React.ReactNode }) {
  const ref = useRef<HTMLDivElement>(null);

  useEffect(() => {
    const el = ref.current;
    if (!el) return;
    if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
      el.dataset["state"] = "in";
      return;
    }
    const io = new IntersectionObserver(
      ([entry]) => {
        if (entry?.isIntersecting) {
          el.dataset["state"] = "in";
          io.unobserve(el);
        }
      },
      { threshold: 0.2 },
    );
    io.observe(el);
    return () => io.disconnect();
  }, []);

  return (
    <div ref={ref} className="scroll-triggered-text-changes" data-state="out">
      {children}
    </div>
  );
}

IF YOU LIKE THIS, STEAL THESE TOO.

INTERACTION_015CSS + JS

Scroll Reveal

Content fades and rises as it enters the viewport.

Scroll / LightView
INTERACTION_016CSS + JS

Blur Reveal

Content sharpens from blur as it enters view.

Scroll / MediumView
INTERACTION_017CSS + JS

Stagger Reveal

Children enter in sequence with a fixed delay step.

Scroll / LightView
INTERACTION_131CSS

Depth Parallax

Three layers move at different speeds to build depth.

Scroll / LightView