Stacking Cards Free React component
Sections that pin as you scroll and pile up like a deck. Each card sticks a little lower than the last, and the ones underneath shrink and dim as new cards slide over them. Great for services, case studies and process steps.
Get the code
npx shadcn@latest add https://alphenex.com/r/stacking-cards.jsonWorks with any React project through the shadcn CLI. Prefer to paste it yourself? Copy the two files below into components/alphenex/. No Tailwind or animation library needed.
import StackingCards from '@/components/alphenex/stacking-cards';
<StackingCards>
<section className="card">Strategy</section>
<section className="card">Design</section>
<section className="card">Build</section>
<section className="card">Launch</section>
</StackingCards>'use client';
import { Children, useEffect, useRef } from 'react';
import './stacking-cards.css';
/** The nearest scrolling ancestor, or null for the page itself. */
function scrollParent(node) {
for (let el = node?.parentElement; el; el = el.parentElement) {
const { overflowY } = getComputedStyle(el);
if (/(auto|scroll|overlay)/.test(overflowY) && el.scrollHeight > el.clientHeight) return el;
}
return null;
}
/**
* StackingCards
* Sections that pin on scroll and pile up like a deck. Each card sticks a
* little lower than the one before it, and the cards underneath shrink and
* dim as new ones slide over them.
*
* Works inside the page or any scrolling box. Put any content in the cards.
*
* Free from Alphenex Lab: https://alphenex.com/lab/stacking-cards (MIT)
*/
export default function StackingCards({
children,
top = 80, // where the first card sticks, px from the top of the scroll area
offset = 24, // how much lower each next card sticks, in px
scaleStep = 0.05, // how much a card shrinks for each card stacked on it
dim = 0.12, // how much a covered card darkens for each card on it
className = '',
style,
...props
}) {
const listRef = useRef(null);
const cards = Children.toArray(children);
useEffect(() => {
const list = listRef.current;
if (!list || window.matchMedia('(prefers-reduced-motion: reduce)').matches) return undefined;
const items = [...list.children];
const scroller = scrollParent(list);
const target = scroller || window;
let frame = 0;
const update = () => {
frame = 0;
const baseTop = scroller ? scroller.getBoundingClientRect().top : 0;
// How far each card has slid into its pinned spot: 0 = not yet, 1 = pinned.
const arrived = items.map((item, i) => {
const rest = baseTop + top + i * offset;
const distance = item.getBoundingClientRect().top - rest;
return Math.min(1, Math.max(0, 1 - distance / item.offsetHeight));
});
items.forEach((item, i) => {
let covered = 0;
for (let j = i + 1; j < items.length; j += 1) covered += arrived[j];
const inner = item.firstElementChild;
if (!inner) return;
inner.style.transform = `scale(${1 - covered * scaleStep})`;
inner.style.filter = covered > 0.01 ? `brightness(${1 - covered * dim})` : '';
});
};
const onScroll = () => {
if (!frame) frame = requestAnimationFrame(update);
};
update();
target.addEventListener('scroll', onScroll, { passive: true });
window.addEventListener('resize', onScroll);
return () => {
target.removeEventListener('scroll', onScroll);
window.removeEventListener('resize', onScroll);
cancelAnimationFrame(frame);
};
}, [top, offset, scaleStep, dim, cards.length]);
return (
<div ref={listRef} className={`alx-stack ${className}`} style={style} {...props}>
{cards.map((card, i) => (
<div key={card.key ?? i} className="alx-stack__item" style={{ top: top + i * offset, zIndex: i + 1 }}>
<div className="alx-stack__card">{card}</div>
</div>
))}
</div>
);
}
/* StackingCards, Alphenex Lab (MIT) */
.alx-stack {
position: relative;
display: grid;
gap: 2rem;
/* Room for the last card to pin before the list scrolls away. */
padding-bottom: 2rem;
}
.alx-stack__item {
position: sticky;
}
.alx-stack__card {
transform-origin: 50% 0;
will-change: transform, filter;
}
Paste this into Claude, ChatGPT, Cursor, Lovable or v0 to build it from scratch.
Build a React "StackingCards" component with no libraries. It wraps each child in a position: sticky item whose top is `top + index * offset`, so the cards pin one slightly below the other and pile up as you scroll. On scroll (throttled with requestAnimationFrame), work out for every card how far it has slid into its pinned spot (0 to 1). Then scale each card down by scaleStep and darken it with filter: brightness() by dim for every card stacked on top of it, using the sum of those 0–1 values so it changes smoothly. Transform an inner wrapper, not the sticky element, with transform-origin at the top centre. Find the nearest scrolling parent so it also works inside a scrolling box, and fall back to the window. Props: top, offset, scaleStep, dim, children. Skip the effect for prefers-reduced-motion.How the stacking cards works
Every card is a position: sticky element with its own top value, the first one at top, the next one offset pixels lower, and so on. That alone makes the cards pin and pile up, with no JavaScript at all.
The depth effect is the JavaScript part. On each scroll frame the component measures how far every card has slid into its pinned spot, from 0 to 1. A card is then shrunk and darkened by the total of those values for all the cards above it in the pile, so the motion is continuous rather than stepped.
It finds the nearest scrolling parent itself, so the same component works for a full page or inside a scrolling panel, like the demo on this page.
Frequently asked questions
Want motion like this on your website?
We design and build custom websites with motion, 3D and SEO built in. Websites from $500.