Ir al contenido
Español
Comparador de imágenes viene con Pro. La vista previa aparece al desbloquearlo.

Cómo funciona

The after layer sizes the component; the before layer sits on top and is clipped on the right with clip-path. The divider and the clip both read one CSS variable, so dragging changes a single value and triggers no layout. Pointer Events with pointer capture keep the drag alive outside the component. On touch, a drag starts only after 6px of sideways movement, so vertical swipes still scroll the page. Labels are small ink-glass pills and the handle is a white knob with a soft glass ring. The demo is a photo-restore card: the comparison sits between a title row and the file with an ink Save button.

Úsalo para

  • Showing the effect of photo restoration, enhancement or filters.
  • Comparing an old and a new design of a page.
  • Before and after results for renovation, repair or cleaning.

Busca otra opción cuando

  • The two images differ in framing: the divider will not line up, so show them side by side.
  • More than two versions: use tabs or a grid.
  • Pixel-level analysis: use a difference overlay.

Uso

import { ImageCompare } from './ImageCompare.jsx'

export function RestorePreview({ rawUrl, fixedUrl }) {
  return (
    <ImageCompare
      style={{ width: 480, aspectRatio: '16 / 10' }}
      before={<img src={rawUrl} alt="Original photo" />}
      after={<img src={fixedUrl} alt="Restored photo" />}
      beforeLabel="Before"
      afterLabel="After"
      aria-label="Comparison position"
    />
  )
}

Propiedades

PropiedadTipoPor defectoQué hace
beforeReactNode—Content shown left of the divider (the original): image, SVG, video or any node.
afterReactNode—Content shown right of the divider (the result). It sizes the component in the document flow.
valuenumber—Controlled position from 0 to 100, as a percentage from the left edge.
defaultValuenumber50Initial position when uncontrolled.
onChange(value: number) => void—Called when the position changes, continuously while dragging.
beforeLabelReactNode'Before'Label in the top-left corner.
afterLabelReactNode'After'Label in the top-right corner.
showLabelsbooleantrueWhether to show the labels.
stepnumber1Percentage moved per arrow key press; multiplied by 10 with Shift.
hintbooleantrueThe first time the slider is half in view, the divider glides 12% toward the larger side and back to show it can be dragged. Only the picture moves; value and onChange are untouched, and any press or key cancels it.
aria-labelstring'Comparison position'Accessible name of the handle (the slider).
...restHTMLAttributes<HTMLDivElement>—Other attributes go to the root element; use style for width and aspect-ratio.

Accesibilidad

  • The handle is a role="slider" with aria-valuenow, aria-valuemin and aria-valuemax; aria-valuetext reads like “Before 50%, After 50%”.
  • Arrow keys move 1% (10% with Shift), PageUp and PageDown 10%, Home and End go to the ends.
  • Pressing anywhere on the component focuses the handle so arrow keys can fine-tune; a focus ring is shown.
  • The handle has a 48px touch target, and vertical swipes on the image still scroll the page.
  • With reduced motion there is no first-view hint, and clicks jump straight to the position without gliding.

Movimiento

First view: the divider glides 12% out and back (720ms each way, ease-in-out) to hint that it can be dragged. Dragging follows the pointer with no transition. Click, Home and End glide over 360ms ease-out; the handle scales to 1.06 on hover and 1.1 while dragging (160ms).

Añádelo a tu proyecto

Los miembros Pro copian un prompt y su agente de programación instala el código, igual que con los gratuitos.

ImageCompare.jsximage-compare.csstokens.cssREADME.md

Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.

Datos
Diferencias de código viene con Pro. La vista previa aparece al desbloquearlo.
Cuenta atrás con flip viene con Pro. La vista previa aparece al desbloquearlo.