Parallax Scroll
Content that drifts at its own speed as it passes through the viewport.
Preview
"use client";
import { useRef } from "react";
import { Parallax } from "@/components/motion/parallax";
// On a real page <Parallax> tracks the viewport. Here it's scoped to the box
// via the container prop. Scroll inside the box: the background image drifts
// against the scroll, the label and avatar drift with it at different speeds.
export function ParallaxPreview() {
const containerRef = useRef<HTMLDivElement>(null);
return (
<div
ref={containerRef}
className="relative h-[600px] w-full max-w-2xl overflow-y-auto scrollbar-hide rounded-2xl border border-border bg-card"
>
<div className="flex h-80 items-center justify-center text-sm text-muted-foreground">
Scroll down ↓
</div>
<div className="relative h-96 overflow-hidden">
<Parallax
container={containerRef}
speed={-0.6}
className="absolute inset-x-0 -top-1/4 h-[150%]"
>
<img
src="https://picsum.photos/seed/beui-parallax/800/600"
alt=""
className="size-full object-cover"
/>
</Parallax>
<Parallax
container={containerRef}
speed={0.5}
className="absolute inset-0 grid place-items-center"
>
<span className="rounded-full bg-background/85 px-5 py-2 text-base font-medium text-foreground backdrop-blur">
Parallax
</span>
</Parallax>
<Parallax
container={containerRef}
speed={0.9}
className="absolute bottom-4 right-4"
>
<img
src="https://picsum.photos/seed/beui-avatar/120/120"
alt=""
className="size-12 rounded-full border-2 border-background object-cover shadow-lg"
/>
</Parallax>
</div>
<div className="flex h-80 items-center justify-center text-sm text-muted-foreground">
↑ Scroll up
</div>
</div>
);
}
"use client";
// beui.dev/components/motion/scroll-animation/parallax
import {
type MotionStyle,
motion,
useReducedMotion,
useScroll,
useSpring,
useTransform,
} from "motion/react";
import { type ReactNode, type RefObject, useRef } from "react";
import { cn } from "@/lib/utils";
// Soft follow so the drift trails the scroll smoothly; looser than the UI
// springs in lib/ease.ts on purpose.
const PARALLAX_SPRING = { stiffness: 120, damping: 30, mass: 0.6 };
export interface ParallaxProps {
children: ReactNode;
/**
* Drift as a fraction of the element's travel through the viewport.
* Positive moves with the scroll (foreground), negative against it
* (background). ~0.1–0.5 reads best.
*/
speed?: number;
/** Drift sideways or up and down as the page scrolls. Default: "y" */
axis?: "x" | "y";
/** Scroll container for contained scroll areas. Defaults to the viewport. */
container?: RefObject<HTMLElement | null>;
/** Spring-smooth the drift. Disabled automatically under reduced motion. */
spring?: boolean;
/** Classes merged onto the root element. */
className?: string;
}
export function Parallax({
children,
speed = 0.3,
axis = "y",
container,
spring = true,
className,
}: ParallaxProps) {
const reduce = useReducedMotion();
const ref = useRef<HTMLDivElement>(null);
const { scrollYProgress } = useScroll({
target: ref,
container,
// A container ref defined higher in the tree may attach after this hook
// runs; useScroll waits for it rather than falling back to the page.
offset: ["start end", "end start"],
});
// progress 0→1 as the element crosses the viewport; map to a symmetric drift.
const travel = speed * 100;
const drift = useTransform(scrollYProgress, [0, 1], [travel, -travel]);
const smoothed = useSpring(drift, PARALLAX_SPRING);
const value = spring && !reduce ? smoothed : drift;
const style: MotionStyle = reduce ? {} : axis === "x" ? { x: value } : { y: value };
return (
<motion.div ref={ref} style={style} className={cn(className)}>
{children}
</motion.div>
);
}
Add it with the shadcn CLI, or copy the source manually.
npx shadcn add https://beui.dev/r/theme.json. Theme setupInstall dependencies
npm i clsx motion tailwind-mergeAdd util file
import { clsx, type ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
Copy the source code
"use client";
// beui.dev/components/motion/scroll-animation/parallax
import {
type MotionStyle,
motion,
useReducedMotion,
useScroll,
useSpring,
useTransform,
} from "motion/react";
import { type ReactNode, type RefObject, useRef } from "react";
import { cn } from "@/lib/utils";
// Soft follow so the drift trails the scroll smoothly; looser than the UI
// springs in lib/ease.ts on purpose.
const PARALLAX_SPRING = { stiffness: 120, damping: 30, mass: 0.6 };
export interface ParallaxProps {
children: ReactNode;
/**
* Drift as a fraction of the element's travel through the viewport.
* Positive moves with the scroll (foreground), negative against it
* (background). ~0.1–0.5 reads best.
*/
speed?: number;
/** Drift sideways or up and down as the page scrolls. Default: "y" */
axis?: "x" | "y";
/** Scroll container for contained scroll areas. Defaults to the viewport. */
container?: RefObject<HTMLElement | null>;
/** Spring-smooth the drift. Disabled automatically under reduced motion. */
spring?: boolean;
/** Classes merged onto the root element. */
className?: string;
}
export function Parallax({
children,
speed = 0.3,
axis = "y",
container,
spring = true,
className,
}: ParallaxProps) {
const reduce = useReducedMotion();
const ref = useRef<HTMLDivElement>(null);
const { scrollYProgress } = useScroll({
target: ref,
container,
// A container ref defined higher in the tree may attach after this hook
// runs; useScroll waits for it rather than falling back to the page.
offset: ["start end", "end start"],
});
// progress 0→1 as the element crosses the viewport; map to a symmetric drift.
const travel = speed * 100;
const drift = useTransform(scrollYProgress, [0, 1], [travel, -travel]);
const smoothed = useSpring(drift, PARALLAX_SPRING);
const value = spring && !reduce ? smoothed : drift;
const style: MotionStyle = reduce ? {} : axis === "x" ? { x: value } : { y: value };
return (
<motion.div ref={ref} style={style} className={cn(className)}>
{children}
</motion.div>
);
}
speed?numberDrift as a fraction of the element's travel through the viewport. Positive moves with the scroll (foreground), negative against it (background). ~0.1–0.5 reads best.
0.3axis?"x" | "y"Drift sideways or up and down as the page scrolls. Default: "y"
ycontainer?RefObject<HTMLElement | null>Scroll container for contained scroll areas. Defaults to the viewport.
—spring?booleanSpring-smooth the drift. Disabled automatically under reduced motion.
trueclassName?stringClasses merged onto the root element.
—Smooth Scroll
Smooth page scrolling with a hook for offset, progress and velocity.
Scroll Progress
A reading progress bar or ring that follows how far the page has scrolled.
Scroll To Button
A button that glides the page to a section, offset or element.
Scroll Reveal
Content that slides in with a soft blur as it enters the viewport.
Tabs
Pill, segment or underline tabs with a spring layoutId indicator and optional icons.
Shared Layout Background
A pill that glides between hovered items via motion's shared layout, with blur enter/exit.
Preview Rail
Codex app-inspired navigation rail with compact ticks that form a hover pyramid and reveal a floating destination preview.
Updated