skip to content
mpkcn

~/components/parallax-image

Parallax Image

An image in a viewfinder frame that drifts with the pointer and with scroll, and holds still under reduced motion.

Mountains at dusk, flat illustration
-- NORMAL --scan_ok

Installation

terminal
npx mpkcn add parallax-image
deps:framer-motion@13.4.4· tested with react@19.3.0

Usage

Pass an image. Move the mouse over the preview and scroll the page to see both layers.

tsx
import { ParallaxImage } from "@/components/mpkcn/parallax-image";

export function Portrait() {
  return (
    <ParallaxImage
      src="/me.jpg"
      alt="Portrait of me at my desk"
      caption={<><span>-- NORMAL --</span><span>scan_ok</span></>}
      className="w-full max-w-md"
    />
  );
}

Two layers move the picture: the pointer layer eases towards the mouse (relative to the window by default, or to the image with pointerArea="element"), and the scroll layer drifts it by scroll pixels while the frame crosses the viewport. The picture is enlarged by the biggest shift so its edges never show.

Examples

Stronger, without corners

Mountains at dusk, flat illustration
-- NORMAL --scan_ok

Anything as the picture

Leave out src and pass children that fill the box: next/image with fill, a canvas, an ImagePlaceholder.

tsx
<ParallaxImage aspectRatio="16 / 9">
  <Image src={hero} alt="…" fill sizes="50vw" className="object-cover" />
</ParallaxImage>

Props

PropTypeDefaultDescription
srcstring—Image URL. Leave it out to pass children.
altstring""Alt text for the image. "" marks it decorative.
childrenReactNode—Content to move instead of an image. Should fill its box.
pointernumber12Largest pointer shift in px. 0 turns it off.
pointerArea"window" | "element""window"What the pointer position is measured against.
scrollnumber40Vertical drift in px across the viewport. 0 turns it off.
cornersbooleantrueDraw the four viewfinder corners.
captionReactNode—Bar at the bottom over the image.
aspectRatiostring | null"4 / 5"CSS aspect-ratio of the frame. null lets className size it.
ignoreReducedMotionbooleanfalseMove even under reduced motion. Only use it behind an explicit action.
classNamestring—Extra classes for the frame.

Accessibility

  • Give the image a real alt, or "" if it is decorative. The corners are hidden from screen readers; the caption is read as text.
  • Under prefers-reduced-motion: reduce both layers stop and the image sits still at its normal size.
  • Touch screens never get the pointer layer. The pointer layer stops its animation frame loop once it has caught up, so a still mouse costs nothing.