# AOS Examples and Integration Patterns Production-ready code examples for AOS (Animate On Scroll) integration across different frameworks and use cases. ## Table of Contents - [Quick Start Templates](#quick-start-templates) - [React Integration](#react-integration) - [Next.js Integration](#nextjs-integration) - [Vue.js Integration](#vuejs-integration) - [Common Patterns](#common-patterns) - [Performance Examples](#performance-examples) - [Accessibility Patterns](#accessibility-patterns) ## Quick Start Templates ### Minimal CDN Setup ```html AOS Minimal Example

Hello World

This content fades in with a delay.

``` ### NPM Setup (ES6) ```bash npm install aos@next ``` ```javascript // main.js import AOS from 'aos'; import 'aos/dist/aos.css'; AOS.init({ duration: 800, once: true, offset: 100 }); ``` ```html
Content here
``` ## React Integration ### Basic React Setup ```jsx // App.jsx import { useEffect } from 'react'; import AOS from 'aos'; import 'aos/dist/aos.css'; function App() { useEffect(() => { AOS.init({ duration: 800, once: true, offset: 100, easing: 'ease-out-cubic' }); }, []); return (

Welcome

Your content here

); } export default App; ``` ### React with React Router ```jsx // App.jsx import { useEffect } from 'react'; import { useLocation, Routes, Route } from 'react-router-dom'; import AOS from 'aos'; import 'aos/dist/aos.css'; import Home from './pages/Home'; import About from './pages/About'; function App() { const location = useLocation(); // Initialize AOS on mount useEffect(() => { AOS.init({ duration: 800, once: true, offset: 100 }); }, []); // Refresh AOS on route change useEffect(() => { AOS.refresh(); }, [location.pathname]); return ( } /> } /> ); } export default App; ``` ### React Wrapper Component ```jsx // components/AnimatedSection.jsx import React from 'react'; const AnimatedSection = ({ children, animation = 'fade-up', delay = 0, duration = 800, offset = 100, once = true, className = '', ...props }) => { return (
{children}
); }; export default AnimatedSection; ``` Usage: ```jsx // pages/Home.jsx import AnimatedSection from '../components/AnimatedSection'; function Home() { return (

Hero Title

Hero subtext

); } ``` ### React with Dynamic Content ```jsx // components/DynamicList.jsx import { useState, useEffect } from 'react'; import AOS from 'aos'; function DynamicList() { const [items, setItems] = useState([]); useEffect(() => { AOS.init({ duration: 600 }); }, []); const addItem = () => { const newItem = { id: Date.now(), text: `Item ${items.length + 1}` }; setItems([...items, newItem]); // Refresh AOS after adding to DOM setTimeout(() => AOS.refresh(), 50); }; const removeItem = (id) => { setItems(items.filter(item => item.id !== id)); setTimeout(() => AOS.refresh(), 50); }; return (
{items.map((item, index) => (
{item.text}
))}
); } export default DynamicList; ``` ## Next.js Integration ### Next.js App Router (_app.js) ```jsx // pages/_app.js import { useEffect } from 'react'; import AOS from 'aos'; import 'aos/dist/aos.css'; import '../styles/globals.css'; function MyApp({ Component, pageProps }) { useEffect(() => { AOS.init({ duration: 800, once: true, offset: 100, easing: 'ease-out-cubic' }); }, []); return ; } export default MyApp; ``` ### Next.js Page Example ```jsx // pages/index.js export default function Home() { return (

Next.js + AOS

Server-side rendered content with scroll animations

{features.map((feature, index) => (

{feature.title}

{feature.description}

))}
); } const features = [ { id: 1, title: 'Fast', description: 'Lightning fast performance' }, { id: 2, title: 'Secure', description: 'Enterprise-grade security' }, { id: 3, title: 'Scalable', description: 'Grows with your needs' } ]; ``` ### Next.js with TypeScript ```tsx // pages/_app.tsx import { useEffect } from 'react'; import type { AppProps } from 'next/app'; import AOS from 'aos'; import 'aos/dist/aos.css'; function MyApp({ Component, pageProps }: AppProps) { useEffect(() => { AOS.init({ duration: 800, once: true }); }, []); return ; } export default MyApp; ``` ```tsx // components/AnimatedCard.tsx interface AnimatedCardProps { title: string; description: string; delay?: number; } const AnimatedCard = ({ title, description, delay = 0 }: AnimatedCardProps) => { return (

{title}

{description}

); }; export default AnimatedCard; ``` ## Vue.js Integration ### Vue 3 Composition API ```vue ``` ### Vue 3 with Dynamic Updates ```vue ``` ### Vue 2 Options API ```vue ``` ## Common Patterns ### Landing Page Pattern ```html AOS Landing Page

Build Something Amazing

The most powerful platform for creating extraordinary experiences

Why Choose Us

🚀

Lightning Fast

Optimized for speed and performance

🎨

Beautiful UI

Stunning designs that users love

🔒

Secure

Enterprise-grade security built-in

``` ### Staggered Grid Pattern ```jsx // React example function ProductGrid({ products }) { useEffect(() => { AOS.init({ duration: 600, once: true }); }, []); return (
{products.map((product, index) => (
{product.name}

{product.name}

{product.price}

))}
); } ``` ### Alternating Content Pattern ```html

Feature One

Description of the feature...

Feature 1
Feature 2

Feature Two

Description of the feature...

``` ## Performance Examples ### Optimized Configuration ```javascript // Best performance configuration AOS.init({ // Animation settings duration: 400, // Shorter = smoother easing: 'ease', // Simple easing // Behavior once: true, // Animate only once (better performance) mirror: false, // Don't animate out // Device settings disable: function() { // Disable on mobile or reduced motion preference return window.innerWidth < 768 || window.matchMedia('(prefers-reduced-motion: reduce)').matches; }, // Performance tuning throttleDelay: 120, // Increase for slower devices debounceDelay: 80, disableMutationObserver: true // For fully static content }); ``` ### Selective Animation ```jsx // Don't animate everything - be selective function BlogPost({ post }) { return (
{/* Animate hero elements */}

{post.title}

{post.author} • {post.date}

{/* Don't animate body text - better for readability */}
{post.content}
{/* Animate CTA */}
); } ``` ## Accessibility Patterns ### Reduced Motion Support ```javascript // Respect user's motion preferences AOS.init({ disable: function() { return window.matchMedia('(prefers-reduced-motion: reduce)').matches; } }); ``` ### Conditional Animation ```jsx // React: Disable animations based on user preference import { useEffect, useState } from 'react'; import AOS from 'aos'; import 'aos/dist/aos.css'; function App() { const [prefersReducedMotion, setPrefersReducedMotion] = useState(false); useEffect(() => { const mediaQuery = window.matchMedia('(prefers-reduced-motion: reduce)'); setPrefersReducedMotion(mediaQuery.matches); const handleChange = () => setPrefersReducedMotion(mediaQuery.matches); mediaQuery.addEventListener('change', handleChange); return () => mediaQuery.removeEventListener('change', handleChange); }, []); useEffect(() => { if (!prefersReducedMotion) { AOS.init({ duration: 800, once: true }); } }, [prefersReducedMotion]); return (

Accessible Heading

); } ``` ### ARIA-friendly Animations ```html

Important Update

This content will be announced to screen readers

``` ## Best Practices Summary ### Do's ✅ Initialize AOS once on app mount ✅ Use `AOS.refresh()` after dynamic content changes ✅ Prefer simpler animations (fade, slide) over complex (flip) ✅ Use `once: true` for better performance ✅ Disable on mobile if not needed ✅ Respect `prefers-reduced-motion` preference ✅ Be selective - don't animate everything ✅ Use consistent animation types per page (2-3 max) ### Don'ts ❌ Don't initialize AOS multiple times ❌ Don't forget to call `AOS.refresh()` after adding elements ❌ Don't animate too many elements at once ❌ Don't use very long durations (>1500ms) ❌ Don't ignore accessibility ❌ Don't animate critical content (nav, forms, body text) ❌ Don't mix too many animation types ## Troubleshooting Checklist - [ ] AOS initialized? (`AOS.init()` called) - [ ] CSS imported? (`import 'aos/dist/aos.css'`) - [ ] Valid `data-aos` attribute? - [ ] Called `AOS.refresh()` after dynamic changes? - [ ] Check browser console for errors - [ ] Try simplified config (duration, once, offset only) - [ ] Verify element is in viewport when scrolling - [ ] Check for CSS conflicts (`opacity`, `transform`) ## Additional Resources - **Scripts**: Use `scripts/aos_generator.py` to generate HTML templates - **Scripts**: Use `scripts/config_builder.py` to build custom configurations - **References**: See `references/aos_api.md` for complete API documentation - **References**: See `references/animation_catalog.md` for all 28 built-in animations - **Official Docs**: https://michalsnik.github.io/aos/