scroll() function creates scroll-linked animations that update based on scroll position. Perfect for parallax effects, scroll progress indicators, and revealing content on scroll.
Function Signature
Parameters
onScroll
Either a callback function or animation controls:ScrollOptions
Return Value
Returns a cleanup function to remove the scroll listener:Basic Usage
Scroll-Linked Animation
- 0% scroll = animation at start
- 100% scroll = animation at end
Scroll Progress Callback
Scroll Info Callback
Scroll Options
Custom Container
Track scroll within a specific container:Horizontal Scroll
Target Element
Animate based on when a specific element scrolls through viewport:Scroll Offsets
Control when the animation starts and ends:["target viewport", "target viewport"]
Positions:
"start"- Top/left edge"center"- Center point"end"- Bottom/right edge- Pixel values:
"100px" - Viewport units:
"50vh","25vw" - Percentages:
"25%" - Numbers:
0.5(50%)
Examples
Parallax Effect
Scroll Progress Bar
Reveal on Scroll
Sticky Scroll Animation
Image Scale on Scroll
Text Reveal
Scroll-Based Counter
Horizontal Scroll Gallery
Fade Through Sections
Advanced Usage
With Stagger Effect
Multiple Properties
Cleanup
Performance Tips
- Use transform properties for smooth animations (x, y, scale, rotate)
- Avoid layout-triggering properties like width, height in scroll animations
- Throttle complex calculations inside scroll callbacks
- Clean up listeners when components unmount
- Use
will-changefor properties that will animate (Motion does this automatically)
ScrollInfo Interface
Browser Support
- Works in all modern browsers
- Uses Scroll Timeline API when available
- Falls back to JavaScript scroll listeners
- Optimized for performance with requestAnimationFrame