GreenSock: Intro to Scrolling Animations (ScrollTrigger)
Master scroll-based animations with GSAP ScrollTrigger plugin
Core ScrollTrigger Concepts
ScrollTrigger Setup
Learn to configure scroll-based animations that trigger when elements enter the viewport. Essential for modern interactive web experiences.
Toggle Actions
Control animation behavior at four key scroll positions: onEnter, onLeave, onEnterBack, and onLeaveBack for complete scroll control.
Scrubbing Effects
Link animations directly to scrollbar position for smooth, interactive scroll experiences that respond to user input in real-time.
You'll animate a heading 'What Makes Hipstirred Coffee Great?' when it scrolls into view, transforming from invisible and scaled down to full visibility and size.
Project Setup Steps
Open Project Folder
Navigate to Desktop > Class Files > JavaScript Class and open the GSAP-ScrollTrigger-Intro folder in your code editor
Launch Files
Open index.html in your code editor and preview it in Chrome browser for testing
Locate Target Element
Scroll down to find the heading 'What Makes Hipstirred Coffee Great?' below the photo - this is what we'll animate
ScrollTrigger is not part of GSAP core and must be loaded separately after the main GSAP library. Always load gsap.min.js first, then ScrollTrigger.min.js.
Script Implementation
Add GSAP Core
Include the main GSAP library script before the closing body tag
Add ScrollTrigger Plugin
Load ScrollTrigger.min.js after the core GSAP script
Create Custom Script Tag
Add an empty script tag below the libraries for your custom animation code
Test Basic Animation
Add a simple gsap.from animation targeting the h2 element with opacity and scale properties
Timeline vs ScrollTrigger Animation Timing
| Feature | Timeline Animation | ScrollTrigger Animation |
|---|---|---|
| Start Trigger | Page load | Element enters viewport |
| User Control | None | Scroll-based |
| Performance | Always running | Context-aware |
Save and reload the page in Chrome to test the new toggle behavior.
ToggleActions provide granular control over animation states as elements move in and out of the viewport from different directions. Our current setting uses restart for the onEnter action, which means the animation will begin fresh each time the element comes into view from below.
- Scroll down and observe how the animation starts when the heading enters the window from below.
- Continue scrolling so the heading moves off the top of the window, then bring it back into view—notice it doesn't animate again because we haven't defined an onEnterBack action.
- Scroll up so the heading disappears off the bottom of the window.
- Scroll back down to see the animation restart every time the heading enters from the bottom, exactly as our configuration specifies.
Configure the second toggle action to pause animations that move off-screen:
scrollTrigger:{
trigger:'h2',
toggleActions:'restart pause none none'
}Save and reload the page in Chrome to test the pause functionality.
We've now set onLeave (when the element scrolls off the top of the window) to pause, which improves performance by stopping animations that users cannot see.
- Scroll down to start the heading animation, but continue scrolling quickly so the heading moves off the top of the screen before the animation completes.
- When the heading leaves the top of the viewport, the animation will pause mid-execution.
- Bring the heading back into view and notice the animation remains paused—we need to configure the resume action next.
Set the third toggle action to resume paused animations:
scrollTrigger:{
trigger:'h2',
toggleActions:'restart pause resume none'
}Save and reload the page in Chrome to test the resume functionality.
We've configured onEnterBack (when the element scrolls into view from the top of the window) to resume, creating seamless animation continuity.
- Scroll down to start the heading animation, then continue scrolling so it moves off the top before completing.
- When the heading leaves the top of the screen, the animation pauses as before.
- Bring the heading back down into view and observe how the animation smoothly resumes from where it left off, rather than starting over.
Monitor the animation in DevTools as you trigger it by bringing the heading into view from the bottom.
During animation, you'll see colored highlighting and dynamic changes in the DevTools Elements panel—this visual feedback confirms when GSAP is actively animating properties. Once the animation completes, the highlighting disappears.
Test performance optimization by triggering the animation again, then quickly scrolling the heading off the bottom of the screen. Notice in DevTools that the animation continues running even when the element is invisible. This unnecessary processing wastes system resources and can impact scroll performance on lower-end devices.
Complete the toggle actions by setting the final action to pause:
scrollTrigger:{
trigger:'h2',
toggleActions:'restart pause resume pause'
}Save and reload the page in Chrome to test the complete toggle action configuration.
We've now set onLeaveBack (when the element scrolls off the bottom of the window) to pause, ensuring optimal performance by stopping animations whenever they move out of view.
- Scroll down to start the heading animation, but quickly scroll back up so the heading moves off the bottom of the screen while still animating.
- Check the DevTools Elements panel—the colored highlighting should stop immediately, confirming GSAP has paused the animation. This performance optimization prevents wasted processing power on invisible animations.
Markers are invaluable for understanding scroll trigger positions during development. Always remove markers:true before deploying to production for clean user experience.
Scrubbing vs Toggle Actions
Set scrub to a time value like 2 seconds instead of true to smooth out jerky animations and create more polished scroll experiences.
Position Configuration
Define Start Position
Set when animation begins using element position relative to viewport. 'top 80%' means element's top hits 80% down from viewport top
Define End Position
Set when animation completes using 'bottom 20%' meaning element's bottom hits 20% down from viewport top
Use Flexible Units
Combine keywords (top, center, bottom) with pixels or percentages for precise control across different screen sizes
Final Implementation Checklist
Clean up development helpers before deployment
Fine-tune smoothness vs responsiveness balance
Ensure animation plays fully within desired scroll range
Confirm scroll triggers work across responsive breakpoints
Key Takeaways
