Scroll Animation
TheScrollAnimation component wraps content to add smooth fade-in and slide-up animations when elements scroll into view. It uses the Intersection Observer API for performance-optimized animations.
Overview
This utility component:- Detects when wrapped content enters the viewport
- Triggers a fade-in and slide-up animation
- Removes the observer after animation to improve performance
- Provides customizable animation duration
Props
The content to animate when it scrolls into view.
Optional additional CSS classes to apply to the wrapper div.
Tailwind duration class for animation speed. Options:
duration-300, duration-500, duration-700, duration-1000.Usage
The component is used on the main page to animate sections as they scroll into view:src/app/page.tsx
Component Implementation
Fromsrc/components/scroll-animation.tsx:
How It Works
Observer setup
Intersection Observer watches for when the element enters the viewport (threshold: 10%).
Animation trigger
When visible, state updates to apply
opacity-100 and translate-y-0, creating a smooth fade-in and slide-up effect.Customizing Animation
Duration
Adjust animation speed with thedelay prop:
Custom Threshold
To modify when the animation triggers, edit thethreshold in the component:
Different Animation Effects
Modify the transition classes to create different effects:Performance Considerations
- Client-side only: Component uses
"use client"directive for hooks - Observer cleanup: Automatically unobserves elements after animation
- Single render: State only updates once when element becomes visible
- Low threshold: 10% visibility threshold ensures smooth user experience
Browser Support
Intersection Observer is supported in all modern browsers:- Chrome 51+
- Firefox 55+
- Safari 12.1+
- Edge 15+
For older browser support, consider adding a polyfill or falling back to CSS-only animations.
Accessibility
- Uses
prefers-reduced-motionmedia query automatically via Tailwind’s transition classes - Content remains accessible to screen readers during animation
- No motion for users who have reduced motion preferences enabled
Related
Theming
Learn about Tailwind animation configuration