Back to library
INTERACTION_055

Magnetic Characters.

Characters are pulled toward the cursor then spring back. Letters at rest in their layout positions before the trigger; offsetting each character toward the pointer by 0.25 of the delta, easing back on leave once pointer movement inside the word bounds.

CATEGORY
Letters
COMPLEXITY
Intermediate
TECH
CSS + JS
PLATFORM
Desktop
PERFORMANCE
Light
LIVE PLAYGROUND
VAULT
CSS / CSS
/* Magnetic Characters — Characters are pulled toward the cursor then spring back. */
.magnetic-characters {
  --dur: 280ms;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
  --stagger: 60ms;
  will-change: transform, opacity;
}

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

/* letters at rest in their layout positions -> offsetting each character toward the pointer by 0.25 of the delta, easing back on leave */
@keyframes magnetic-characters-in {
  to {
    opacity: 1;
    transform: none;
    filter: none;
  }
}

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

/** Magnetic Characters — trigger: pointer movement inside the word bounds */
export function MagneticCharacters({ 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="magnetic-characters" data-state="out">
      {children}
    </div>
  );
}

IF YOU LIKE THIS, STEAL THESE TOO.

VAULT
INTERACTION_050CSS + JS

Scattered Letters Assemble

Characters fly in from random offsets and lock into the word.

Letters / LightView
VAULT
INTERACTION_051CSS

Letter Flip

Characters flip on the X axis like a departure board.

Letters / LightView
VAULT
INTERACTION_053CSS

Vertical Character Roll

Each character rolls up inside its own clipped slot.

Letters / LightView
VAULT
INTERACTION_054CSS + JS

Cursor Reactive Letters

Characters lift and brighten as the pointer passes over them.

Letters / LightView