A silky Before/After image comparison slider using GPU-accelerated CSS clip-path and pointer capture — ideal for comparing daylight vs. dusk lighting, architectural renders vs. blueprints, or product retouching.
'use client'
import { useState } from 'react'
export function BeforeAfterSlider({ beforeSrc, afterSrc }: { beforeSrc: string; afterSrc: string }) {
const [pos, setPos] = useState(52)
const handlePointer = (e: React.PointerEvent<HTMLDivElement>) => {
if (e.buttons !== 1 && e.type === 'pointermove') return
const r = e.currentTarget.getBoundingClientRect()
setPos(Math.max(4, Math.min(96, ((e.clientX - r.left) / r.width) * 100)))
}
return (
<div
onPointerDown={handlePointer}
onPointerMove={handlePointer}
className="relative aspect-[16/10] w-full cursor-ew-resize overflow-hidden rounded-2xl select-none touch-none"
>
<img src={afterSrc} alt="After" className="absolute inset-0 h-full w-full object-cover" />
<img
src={beforeSrc}
alt="Before"
style={{ clipPath: `inset(0 ${100 - pos}% 0 0)` }}
className="absolute inset-0 h-full w-full object-cover"
/>
<div style={{ left: `${pos}%` }} className="absolute inset-y-0 w-0.5 bg-white shadow">
<div
style={{ borderColor: '#b45309' }}
className="absolute left-1/2 top-1/2 grid h-10 w-10 -translate-x-1/2 -translate-y-1/2 place-items-center rounded-full border-2 bg-white font-mono text-sm font-bold text-stone-900 shadow-lg"
>
⇆
</div>
</div>
</div>
)
}Loading single-file HTML…
Build a single-file draggable Before/After Split Comparison Slider using pure HTML, CSS clip-path, and vanilla JS on warm architectural stone (#f3efe7). - Comparison Craft: Stack two full-bleed layers (Daylight 10:30 AM architectural elevation over Warm Twilight 07:15 PM lighting) and reveal the top layer cleanly via clip-path: inset(0 calc(100% - var(--pos)) 0 0) with a crisp vertical white divider, circular drag handle (#b45309 border), and floating frosted time badges. - Interaction & A11y: Use setPointerCapture for silky mouse and mobile touch dragging plus full keyboard ArrowLeft/ArrowRight slider accessibility (role="slider").
WHY THIS WORKS IN PRODUCTION
- Silky GPU CSS clip-path reveal — works with any two <img> or <video> layers
- Pointer Capture ensures smooth dragging on mobile touchscreens
- Accessible ARIA role="slider" with ArrowLeft / ArrowRight keyboard control
- Copy-paste <BeforeAfterSlider /> React + Tailwind TSX component included
DROP-IN SPECS // WEB & MOBILE FIT









