Parallax scrolling
Also called parallax effect
A scroll effect where background layers move slower than the foreground, which creates a sense of depth as the page scrolls.
import { useRef } from 'react';
import {
motion,
useReducedMotion,
useScroll,
useTransform
} from 'motion/react';
// Fraction of the scroll speed each far layer moves at. 1 is normal speed.
const SKY_SPEED = 0.2;
const MOUNTAINS_SPEED = 0.5;
const LAYER = 'absolute inset-0 size-full object-cover';
export function ParallaxHero() {
const ref = useRef<HTMLElement>(null);
const reduceMotion = useReducedMotion();
// 0 with the hero's top at the top of the viewport, 1 once it has scrolled out.
const { scrollYProgress } = useScroll({
target: ref,
offset: ['start start', 'end start']
});
// Pushing a layer down while the page scrolls up makes it move slower on screen.
const skyY = useTransform(
scrollYProgress,
[0, 1],
['0%', `${(1 - SKY_SPEED) * 100}%`]
);
const mountainsY = useTransform(
scrollYProgress,
[0, 1],
['0%', `${(1 - MOUNTAINS_SPEED) * 100}%`]
);
return (
<section ref={ref} className='relative h-screen overflow-hidden'>
<motion.img
src='/hero/sky.webp'
alt=''
style={reduceMotion ? undefined : { y: skyY }}
className={LAYER}
/>
<motion.img
src='/hero/mountains.webp'
alt=''
style={reduceMotion ? undefined : { y: mountainsY }}
className={LAYER}
/>
<img src='/hero/foreground.webp' alt='' className={LAYER} />
<h1 className='relative pt-32 text-center text-5xl font-semibold text-white'>
Into the mountains
</h1>
</section>
);
}Add parallax scrolling to the hero: split the illustration into three layers (sky, mountains, foreground) that move at 20%, 50%, and 100% of the scroll speed as the hero scrolls out of view. Drive transform: translateY from scroll position, keep the headline at normal speed, and disable the effect when prefers-reduced-motion is set.
Paste into Layerwise or any AI coding tool.
What is Parallax scrolling?
Parallax scrolling borrows a trick from side-scrolling games: things far away move slowly and things up close move fast. On a web page, a hero illustration might scroll at a fraction of normal speed while the headline and content scroll normally, so the scene feels layered.
It works best in a hero or one illustrated section where depth adds to the story. Use two to four layers with clearly different speeds, and keep text on a layer that moves at normal speed so it stays easy to read.
Strong or constant parallax can make some people feel motion sick, so turn it off when prefers-reduced-motion is set. Move layers with transform; the older background-attachment: fixed trick is ignored by many mobile browsers.
Related terms
All termsScroll-driven animation
An animation whose progress is tied directly to the scroll position, so it moves forward as you scroll down and rewinds as you scroll up.
Hero section
The large first section at the top of a landing page, with a headline, a short subhead, a call to action, and a visual.
Perspective
A CSS property that sets how far the viewer sits from a 3D scene, so rotated elements look deep up close and flatter from far away.
Reduced motion
An operating system setting that asks apps to minimize movement, which websites detect with the prefers-reduced-motion media query.
Build it with Layerwise
Describe your app, watch it run in a live preview, and publish it with a database, storage, and sign-in built in.
