Momentum
Also called inertia, inertial scrolling, or fling
The glide an element keeps after a throw or flick, slowing down gradually like an object sliding across a smooth table.
import { animate, motion, useMotionValue } from 'motion/react';
const SLIDE_WIDTH = 320;
const SNAP = { type: 'spring', stiffness: 300, damping: 35 } as const;
const photos = [
{ src: '/photos/lisbon.jpg', alt: 'A yellow tram on a hill in Lisbon' },
{ src: '/photos/kyoto.jpg', alt: 'A temple path in Kyoto' },
{ src: '/photos/oaxaca.jpg', alt: 'A colorful street in Oaxaca' }
];
export function PhotoCarousel() {
const x = useMotionValue(0);
function step(direction: 1 | -1) {
const index = Math.round(-x.get() / SLIDE_WIDTH) + direction;
const clamped = Math.min(Math.max(index, 0), photos.length - 1);
animate(x, -clamped * SLIDE_WIDTH, SNAP);
}
return (
<div
tabIndex={0}
role='region'
aria-roledescription='carousel'
aria-label='Trip photos'
onKeyDown={(event) => {
if (event.key === 'ArrowRight') step(1);
if (event.key === 'ArrowLeft') step(-1);
}}
className='w-80 overflow-hidden rounded-2xl outline-none focus-visible:ring-2 focus-visible:ring-ring'
>
<motion.div
drag='x'
style={{ x }}
dragConstraints={{ left: -(photos.length - 1) * SLIDE_WIDTH, right: 0 }}
dragElastic={0.2}
dragTransition={{
power: 0.3, // how far a flick carries
timeConstant: 300, // how long the glide lasts, in ms
// Glide toward the nearest slide instead of stopping between two.
modifyTarget: (target) =>
Math.round(target / SLIDE_WIDTH) * SLIDE_WIDTH
}}
className='flex cursor-grab touch-pan-y active:cursor-grabbing'
>
{photos.map((photo) => (
<img
key={photo.src}
src={photo.src}
alt={photo.alt}
draggable={false}
className='h-56 w-80 shrink-0 object-cover'
/>
))}
</motion.div>
</div>
);
}Make the photo carousel draggable with momentum: after a flick, let it glide and slow down over about 300ms, then snap to the nearest slide. Stop at the first and last slides with a small elastic bounce, and support touch, mouse, and arrow keys.
Paste into Layerwise or any AI coding tool.
What is Momentum?
With momentum, a dragged element keeps moving after you let go. It travels in the direction of the throw at the speed of your hand, then slows smoothly to a stop. A harder flick travels further. Scrolling on phones and trackpads works this way.
Use it for things people throw: carousels, swipeable cards, maps, and canvases. Combine it with snap points so the element lands on a sensible spot, such as the nearest slide, and with an elastic edge so a throw past the end bounces back.
Motion’s drag gesture adds momentum by default (dragMomentum), and dragTransition tunes it: power sets how far a throw carries, and timeConstant sets how long the glide lasts. Turn it off for precise controls, where an item should stay exactly where it was dropped.
Often confused with
- Velocity
- Velocity is the speed at the moment of release. Momentum is the glide that speed produces afterward.
- Smooth scrolling
- Smooth scrolling eases the page toward each wheel turn or key press. Momentum is the glide that continues after a flick.
Related terms
All termsVelocity
The speed and direction an element already has when an animation starts, which a spring carries forward so a flick flows into what follows.
Drag gesture
Moving an element by pressing on it and sliding the pointer or finger, usually within set bounds and with a glide after release.
Rubber-banding
An edge effect where content dragged past its limit moves less than your finger, then springs back on release, like a stretched rubber band.
Scroll snap
A CSS feature that makes a scroll container settle on set points, so carousels and full-screen sections come to rest neatly on an item.
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.