GreenSock: ScrollTrigger on Multiple Alternating Elements
Master GSAP ScrollTrigger for dynamic web animations
GSAP ScrollTrigger Core Concepts
Individual Element Targeting
Learn to apply ScrollTrigger animations to multiple elements separately, avoiding the common mistake of animating all elements simultaneously.
Dynamic Direction Control
Master alternating animation directions for each row, creating engaging visual patterns that draw elements toward the center.
Performance Optimization
Discover techniques to prevent unwanted horizontal scrolling and maintain smooth animations across different viewport sizes.
This tutorial creates alternating scroll-triggered animations where images and text slide toward each other from opposite directions, creating a dynamic storytelling effect as users scroll through content sections.
Initial Setup Process
Open Project Files
Navigate to Desktop > Class Files > JavaScript Class > GSAP-ScrollTrigger-Multiple folder and open in your code editor
Preview the Layout
Open index.html in browser and scroll to observe the alternating sections with photos and text that need animation
Prepare Development Environment
Ensure GSAP core and ScrollTrigger plugins are properly linked and ready for animation implementation
Using a single ScrollTrigger for multiple elements causes all elements to animate simultaneously. This is one of the most frequent errors developers make when starting with ScrollTrigger.
ScrollTrigger Configuration Options
Trigger Points
Set start and end points using percentages like 'top 85%' and 'bottom 80%' to control when animations begin and complete.
Scrub Animation
Link animation progress directly to scroll position using scrub values, creating smooth scroll-controlled motion effects.
Individual Element Animation Setup
Create Element Array
Use document.querySelectorAll('section img') to create a collection of all target images
Implement For Loop
Wrap the ScrollTrigger animation in a for loop to iterate through each image individually
Target Individual Elements
Replace string selectors with array elements using sectionImg[i] to ensure each image gets its own trigger
The modulo operator (%) determines if array index i is even or odd. Even numbers (i % 2 == 0) have no remainder, while odd numbers do, enabling alternating animation directions.
Direction Logic Implementation
Even Row Animation
Array index 0, 2, 4 etc. use negative movement values to animate elements from left toward center.
Odd Row Animation
Array index 1, 3, 5 etc. use positive movement values to animate elements from right toward center.
Text Animation Integration
Create Text Element Array
Add let sectionText = document.querySelectorAll('section.text') to target all text sections
Duplicate Animation Code
Copy the image animation gsap.from() method and modify it for text elements
Apply Opposite Direction
Use -movement for text animation to create converging motion where text and images move toward each other
Keep the markers property in your code even when set to false. This allows quick debugging by switching back to true when troubleshooting animation timing issues.
CSS Overflow Hidden Solution
Key Takeaways
