Design Converter
Education
Last updated on Mar 7, 2025
•12 mins read
Last updated on Mar 7, 2025
•12 mins read
Looking to create smooth, scroll-based animations that keep users engaged?
ScrollTrigger makes it easy. This powerful GSAP plugin lets developers add eye-catching effects with minimal code. From basic triggers to advanced animations, it offers plenty of options to bring web pages to life.
This blog breaks down how ScrollTrigger works, covering key features, setup steps, and useful techniques. Whether it's pinning elements, adding parallax effects, or syncing animations with scroll positions, this tool opens up endless possibilities.
Let’s get started!
ScrollTrigger is a plugin that allows you to create scroll-based animations. It can be used to trigger animations when elements become visible on the page, control the animation playhead based on the scroll position, and even pin elements in place during scrolling. This versatility makes ScrollTrigger an invaluable tool for creating dynamic and engaging web experiences.
ScrollTrigger stands out due to its flexibility and ease of use. Whether you're looking to create simple scroll-based triggers or complex animations that follow the scrollbar, ScrollTrigger has you covered. Its integration with GSAP ensures high performance and compatibility across all major browsers.
At its core, ScrollTrigger works by defining a start and end point within the scrollable area. When the scroll position reaches these points, the associated animation is triggered. This can be as simple as fading in an element or as complex as creating a parallax scrolling effect.
Before you can use ScrollTrigger, you need to register it in your project. This can be done by including the ScrollTrigger plugin in your JavaScript file. Here's how you can do it:
1gsap.registerPlugin(ScrollTrigger);
You may alter the trigger's activation time and animation effect by adjusting the scrollTrigger property's various parameters.
Here's a basic example:
1gsap.to(".box", { 2 x: 500, 3 duration: 2, 4 scrollTrigger: { 5 trigger: ".box", 6 start: "top center", 7 end: "bottom top", 8 markers: true 9 } 10});
In this example, the animation will start when the top of the trigger element hits the center of the viewport and end when the bottom of the trigger element hits the top of the viewport. The markers property is set to true to display visual indicators for debugging purposes.
Providing a string value to scrollTrigger is equivalent to passing an object with the trigger property. For example:
1gsap.to(".box", { 2 x: 500, 3 duration: 2, 4 scrollTrigger: ".box" 5});
This is equivalent to:
1gsap.to(".box", { 2 x: 500, 3 duration: 2, 4 scrollTrigger: { 5 trigger: ".box" 6 } 7});
The element whose location the ScrollTrigger watches can be specified using the trigger option. It can take a DOM element object or a CSS selector string. For instance:
1gsap.to(".box", { 2 x: 500, 3 duration: 2, 4 scrollTrigger: { 5 trigger: document.querySelector(".box") 6 } 7});
The start option allows you to specify exactly when the ScrollTrigger will start the animation. Its value is "top bottom" by default, meaning that when the top of the trigger element touches the bottom of the screen, the animation will begin. However, you can use more complex values like "bottom 80%", which means the animation will start when the bottom of the trigger element hits 80% down from the top of the viewport.
1gsap.to(".box", { 2 x: 500, 3 duration: 2, 4 scrollTrigger: { 5 trigger: ".box", 6 start: "bottom 80%" 7 } 8});
The end option allows you to specify where the ScrollTrigger should end. It uses the same syntax as start. For example:
1gsap.to(".box", { 2 x: 500, 3 duration: 2, 4 scrollTrigger: { 5 trigger: ".box", 6 start: "top center", 7 end: "bottom top" 8 } 9});
In this example, the animation will end when the bottom of the trigger element hits the top of the viewport.
ToggleActions is an important concept to master when working with ScrollTrigger animations. It allows you to specify what should happen to your animation in the following cases: play, pause, resume, reset. The default value for toggleActions is "play none none none", meaning the animation will play once when the element enters the view and do nothing else.
1gsap.to(".box", { 2 x: 500, 3 duration: 2, 4 scrollTrigger: { 5 trigger: ".box", 6 start: "top center", 7 end: "bottom top", 8 toggleActions: "play pause resume reset" 9 } 10});
The pin option allows you to pin an element in place while the ScrollTrigger is active. Setting pin to true will cause it to pin the trigger element.
1gsap.to(".box", { 2 x: 500, 3 duration: 2, 4 scrollTrigger: { 5 trigger: ".box", 6 start: "top center", 7 end: "bottom top", 8 pin: true 9 } 10});
Using pinning can lead to performance issues if there are transforms on parent elements, so use it judiciously.
The start property allows us to set a specific point in the viewport where the animation starts. We can use numeric values, string values, or keywords to set the start property. For example, we can use the keywords "top" and "center" to trigger the animation when the trigger element reaches the center of the viewport.
1gsap.to(".box", { 2 x: 500, 3 duration: 2, 4 scrollTrigger: { 5 trigger: ".box", 6 start: "top center" 7 } 8});
GSAP provides a handy property called markers to help us set start and end points visually. Using markers as a boolean adds default visual indicators for the start and end of a ScrollTrigger.
1gsap.to(".box", { 2 x: 500, 3 duration: 2, 4 scrollTrigger: { 5 trigger: ".box", 6 start: "top center", 7 end: "bottom top", 8 markers: true 9 } 10});
Customizing markers can enhance the debugging process by changing their appearance and properties.
ScrollTrigger allows you to control the direction of the animation based on the scroll direction. By default, the animation will play forward when scrolling down and reverse when scrolling up. However, you can customize this behavior using the toggleActions property.
1gsap.to(".box", { 2 x: 500, 3 duration: 2, 4 scrollTrigger: { 5 trigger: ".box", 6 start: "top center", 7 end: "bottom top", 8 toggleActions: "play reverse play reverse" 9 } 10});
In this example, the animation will play forward when the trigger element enters the viewport and reverse when it leaves the viewport, both when scrolling down and up.
The scrub property allows you to create smooth animations that follow the scroll position precisely. When scrub is set to true, the animation will scrub back and forth based on the scroll position, creating a seamless and fluid effect.
1gsap.to(".box", { 2 x: 500, 3 duration: 2, 4 scrollTrigger: { 5 trigger: ".box", 6 start: "top center", 7 end: "bottom top", 8 scrub: true 9 } 10});
You can also fine-tune the scrubbing effect by setting scrub to a numeric value. For example, scrub: 2 will make the animation scrub twice as fast as the scroll speed.
A common technique for adding depth is parallax scrolling, in which background components move at a different speed than foreground objects. ScrollTrigger makes it easy to achieve this effect with minimal code.
1gsap.to(".background", { 2 y: -200, 3 scrollTrigger: { 4 trigger: ".background", 5 start: "top top", 6 end: "bottom top", 7 scrub: true 8 } 9}); 10 11gsap.to(".foreground", { 12 y: -100, 13 scrollTrigger: { 14 trigger: ".foreground", 15 start: "top top", 16 end: "bottom top", 17 scrub: true 18 } 19});
In this example, the background element will move at half the speed of the foreground element, creating a parallax effect.
ScrollTrigger allows you to animate multiple elements simultaneously or sequentially. You can achieve this by creating a timeline and adding multiple animations to it.
1const tl = gsap.timeline({ 2 scrollTrigger: { 3 trigger: ".container", 4 start: "top center", 5 end: "bottom top", 6 scrub: true 7 } 8}); 9 10tl.to(".element1", { x: 500, duration: 2 }) 11 .to(".element2", { y: -300, duration: 2 }, "-=1") 12 .to(".element3", { rotation: 360, duration: 2 }, "-=1");
In this example, element1 will animate first, followed by element2 and element3, with overlapping durations for a smooth transition.
Creating responsive scroll-based animations is crucial for ensuring a consistent user experience across different devices. ScrollTrigger allows you to use media queries to create different setups for various screen sizes.
1ScrollTrigger.matchMedia({ 2 "(min-width: 800px)": function() { 3 gsap.to(".box", { 4 x: 500, 5 duration: 2, 6 scrollTrigger: { 7 trigger: ".box", 8 start: "top center", 9 end: "bottom top" 10 } 11 }); 12 }, 13 "(max-width: 799px)": function() { 14 gsap.to(".box", { 15 x: 300, 16 duration: 1.5, 17 scrollTrigger: { 18 trigger: ".box", 19 start: "top bottom", 20 end: "bottom top" 21 } 22 }); 23 } 24});
In this example, the animation will have different start and end points, as well as different durations, based on the screen width.
By default, ScrollTrigger uses the viewport as the scroll container. However, you can define a custom scroller, such as a <div>
, to create scroll-based animations within a specific container.
1gsap.to(".box", { 2 x: 500, 3 duration: 2, 4 scrollTrigger: { 5 trigger: ".box", 6 start: "top center", 7 end: "bottom top", 8 containerAnimation: { 9 container: "#custom-container" 10 } 11 } 12});
In this example, the animation will be triggered based on the scroll position within the #custom-container element.
While the default markers are helpful for debugging, you can customize them further to better suit your needs. For example, you can change the color, size, and labels of the markers.
1gsap.to(".box", { 2 x: 500, 3 duration: 2, 4 scrollTrigger: { 5 trigger: ".box", 6 start: "top center", 7 end: "bottom top", 8 markers: { 9 startColor: "green", 10 endColor: "red", 11 fontSize: "20px", 12 indent: 20 13 } 14 } 15});
In this example, the start marker will be green, the end marker will be red, the font size will be 20px, and the markers will be indented by 20 pixels.
The duration property in GSAP animations controls the overall speed of the animation. However, when using ScrollTrigger, you can fine-tune the animation speed based on the scroll position.
1gsap.to(".box", { 2 x: 500, 3 duration: 2, 4 scrollTrigger: { 5 trigger: ".box", 6 start: "top center", 7 end: "bottom top", 8 scrub: true, 9 duration: 3 10 } 11});
In this example, the animation will take 3 seconds to complete, but the scrub property will ensure it follows the scroll position precisely.
The endTrigger property allows you to specify a different element for the end point of the ScrollTrigger. This can be useful when you want the animation to start based on one element but end based on another.
1gsap.to(".box", { 2 x: 500, 3 duration: 2, 4 scrollTrigger: { 5 trigger: ".box", 6 start: "top center", 7 endTrigger: ".end-element", 8 end: "bottom top" 9 } 10});
In this example, the animation will start when the top of the .box element hits the center of the viewport and end when the bottom of the .end-element hits the top of the viewport.
While ScrollTrigger is highly optimized, there are a few best practices to ensure your animations run smoothly:
Debounce Scroll Events: ScrollTrigger automatically debounces scroll events to improve performance. However, you can further optimize by limiting the number of animations triggered simultaneously.
Avoid Complex Animations: Complex animations with many properties can be resource-intensive. Try to simplify your animations where possible.
Use CSS for Static Styles: Whenever possible, use CSS for static styles and GSAP for dynamic animations. This helps reduce the load on the JavaScript engine.
Lazy Loading: Load your animations only when they are needed. For example, you can use the IntersectionObserver API to trigger animations only when elements are in the viewport.
Even with its ease of use, there are some common pitfalls to watch out for when using ScrollTrigger:
Forgetting to Register the Plugin: Always ensure you register ScrollTrigger with gsap.registerPlugin(ScrollTrigger);
before using it.
Incorrect Trigger Elements: Double-check that your trigger elements are correctly selected and exist in the DOM.
Conflicting Animations: Be mindful of conflicting animations that might interfere with each other. Use unique selectors and carefully plan your animation sequences.
Performance Issues: Complex animations or excessive use of ScrollTrigger can lead to performance issues. Optimize your animations and use performance monitoring tools to identify bottlenecks.
To see ScrollTrigger in action, let's look at a few real-world examples:
Parallax Scrolling: Create a parallax scrolling effect where background images move at a different speed than the foreground content. This can be achieved by setting different scrub values for background and foreground elements.
Sticky Sections: Use the pin property to create sticky sections that remain in place while the user scrolls through other content. This is great for highlighting important information or creating engaging storytelling experiences.
Interactive Infographics: Animate infographics to reveal data as the user scrolls. This can make complex information more engaging and easier to understand.
Product Showcases: Create dynamic product showcases where products animate into view as the user scrolls. This can include rotations, scaling, and other effects to make the products stand out.
ScrollTrigger makes scroll-based animations smooth and fun to create. Since it works with GSAP, it helps bring animations to life with less code. With the right setup and a bit of creativity, it can turn a simple page into an engaging experience.
From basic triggers to advanced parallax effects, ScrollTrigger gives developers the flexibility to build interactive designs. The best way to get comfortable with it is to try different features and see what works. So, keep experimenting and make the most of what ScrollTrigger has to offer!
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.