Back to library
INTERACTION_106

Before And After Slider.

A draggable handle wipes between two versions of an image. The handle at 50% with the after image clipped before the trigger; updating the clip inset to follow the handle position once dragging the handle or moving the pointer across the frame.

CATEGORY
Images
COMPLEXITY
Intermediate
TECH
CSS + JS
PLATFORM
Both
PERFORMANCE
Light
LIVE PLAYGROUND
CSS / CSS
/* Before And After Slider — A draggable handle wipes between two versions of an image. */
.before-after-slider {
  --dur: 120ms;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
  --stagger: 60ms;
  will-change: transform, opacity;
}

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

/* the handle at 50% with the after image clipped -> updating the clip inset to follow the handle position */
@keyframes before-after-slider-in {
  to {
    opacity: 1;
    transform: none;
    filter: none;
  }
}

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

/** Before And After Slider — trigger: dragging the handle or moving the pointer across the frame */
export function BeforeAndAfterSlider({ 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="before-after-slider" data-state="out">
      {children}
    </div>
  );
}

IF YOU LIKE THIS, STEAL THESE TOO.

INTERACTION_113CSS

Cinematic Image Entrance

The image arrives with a letterbox open and a slow push-in.

Images / LightView
INTERACTION_115CSS

Layered Image Depth

Foreground, subject and background layers move at different rates.

Images / LightView
INTERACTION_105CSS

Image Mask Reveal

The image uncovers behind a mask while it settles from a slow zoom.

Images / LightView
Media
INTERACTION_107CSS

Parallax Image

The image drifts slower than the frame that contains it.

Images / LightView