Try 'magnetic button'
View code

Specification

Catalogue
No. 3 of 110
Interaction
Drag
Element
Media
Preview
Live component
Imports
react 19.3.1
Added

Notes

Drag one line across two photos of the same place to see what changed.

Proof the visitor operates themselves, which beats a claim. Both photos are labelled the whole time, the buttons show either one in full, and nothing slides on its own.

Follows the pointer or finger exactly while dragging; a tap jumps the line there. Show before or after glides 450ms, cubic-bezier(.77, 0, .175, 1). A native range input underneath: arrow keys move 1%, Page Up and Down 10%. Changed from the library: no sparkles on the line, no autoplay sweep, labels always visible.

The names, figures and pictures in the demo are sample content; a real build uses the client's own.

Code

React and plain CSS
"use client";

import { useState, type CSSProperties } from "react";
import { reducedNow } from "@/components/pieces/motion-helpers";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "./compare.css";

const S = "/samples";

function CompareDemo() {
  const [pos, setPos] = useState(50);
  const [glide, setGlide] = useState(false);
  const show = (v: number) => {
    setGlide(!reducedNow());
    setPos(v);
  };
  return (
    <div className="dm">
      <div className="cmp" style={{ "--pos": `${pos}%` } as CSSProperties} data-glide={glide || undefined}>
        <img className="cmp-img" src={`${S}/kitchen-oak.svg`} alt="After: the same kitchen with oak units, a white tiled splashback and pendant lights" draggable={false} />
        <div className="cmp-before">
          <img className="cmp-img" src={`${S}/kitchen-before.svg`} alt="Before: a dated cream kitchen with a cupboard door hanging off its hinge" draggable={false} />
        </div>
        <span className="cmp-label" data-side="before">
          Before
        </span>
        <span className="cmp-label" data-side="after">
          After
        </span>
        <input
          className="cmp-range"
          type="range"
          min={0}
          max={100}
          value={pos}
          aria-label="Before and after: move to compare"
          aria-valuetext={`${pos}% of the before photo shown`}
          onPointerDown={() => setGlide(false)}
          onChange={(e) => {
            setGlide(false);
            setPos(Number(e.target.value));
          }}
        />
        <div className="cmp-handle" aria-hidden="true">
          <span className="cmp-grip">
            <svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
              <path d="m9 7-5 5 5 5M15 7l5 5-5 5" />
            </svg>
          </span>
        </div>
      </div>
      <div className="dm-actions">
        <button type="button" className="dm-btn" onClick={() => show(100)} aria-pressed={pos === 100}>
          Show before
        </button>
        <button type="button" className="dm-btn" onClick={() => show(0)} aria-pressed={pos === 0}>
          Show after
        </button>
      </div>
    </div>
  );
}

// What a hovered card shows: the piece used the way a visitor would.
const TOUR: Tour = [
  { input: ".cmp-range", value: "18", wait: 900 },
  { input: ".cmp-range", value: "82", wait: 900 },
  { input: ".cmp-range", value: "50", wait: 600 },
];

export default function CompareEntry(props: LivePreviewProps) {
  return (
    <PieceStage {...props} label="Before and after slider" sample tour={TOUR}>
      <CompareDemo />
    </PieceStage>
  );
}

More like this

Image panels that widen

Hover, Layout AnimationMedia and galleries

Photo grid that opens into the photo

Layout AnimationMedia and galleries

A photo for each option

PressTabs

Product you can turn

DragMedia and galleries

Bookshelf

Press, Layout AnimationMedia and galleries

Header that steps aside

ScrollNavigation