JavaScript Animation - GSAP and ScrollTrigger
Gain Extensive Insight into GSAP, ScrollTrigger, and Popular Animation Libraries

At Code Subtle, we empower aspiring web developers through personalized mentorship and engaging learning resources. Our community bridges the gap between theory and practice, guiding students from basics to advanced concepts. We offer expert mentorship and write interactive, user-friendly articles on all aspects of web development. Join us to learn, grow, and build your future in tech!
GSAP (GreenSock Animation Platform)
1. What is GSAP?
GSAP (GreenSock Animation Platform) is a high-performance JavaScript library for creating professional-grade animations. Think of it as the Swiss Army knife for web animation. It's incredibly fast, cross-browser compatible, and allows you to animate almost anything you can think of—from CSS properties and SVG to generic JavaScript objects.
Why choose GSAP?
Performance: It's built for speed and efficiency, ensuring your animations are buttery smooth.
Flexibility: Animate colors, positions, sizes, rotations, and much more with a simple, intuitive syntax.
Compatibility: It handles browser inconsistencies for you, so your animations work flawlessly everywhere.
2. How to Connect GSAP CDN in a Project
Getting started is as simple as adding a script tag to your HTML file. You don't need to install anything. Just grab the CDN link from the GreenSock website.
Place this script tag right before your closing </body> tag, and make sure it comes before your own JavaScript file (script.js).
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GSAP Basics</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- Your HTML content -->
<div class="box"></div>
<!-- GSAP CDN -->
<script src="[https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.5/gsap.min.js](https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.5/gsap.min.js)"></script>
<!-- Your JS File -->
<script src="script.js"></script>
</body>
</html>
3. Working of GSAP
GSAP's power lies in its simple and readable methods, often called "tweens." A tween is a single animation. Let's look at the most common ones.
3.1 gsap.to()
The gsap.to() method is the most fundamental and widely used tween. It animates an element TO a new state. You define the target element and the properties you want it to have at the end of the animation.
Syntax:
gsap.to("target", {varsObject});target: The CSS selector for the element you want to animate (e.g.,".box","#header").varsObject: A JavaScript object containing the properties you want to animate (the destination values) and other options likeduration,delay, etc.
Example: Let's move a box 500 pixels to the right over 2 seconds.
// In your script.js file
gsap.to(".box", {
x: 500, // Move 500px on the x-axis
duration: 2, // Animation duration in seconds
rotation: 360, // Rotate 360 degrees
backgroundColor: "#ff4500", // Animate to OrangeRed
delay: 1 // Wait 1 second before starting
});
Here, the box will start at its original position and animate to the new state defined in the object.
3.2 gsap.from()
The gsap.from() method works in the opposite way. It animates an element FROM a specified state to its current state (as defined in your CSS). This is perfect for animating elements into the screen.
- Syntax:
gsap.from("target", {varsObject});
Example: Let's make a heading fade in and slide up from below.
gsap.from("h1", {
y: 100, // Start 100px below its final position
opacity: 0, // Start completely transparent
duration: 1.5, // Animation duration
ease: "power4.out" // An easing function for a smooth effect
});
In this case, the h1 element will instantly be set to opacity: 0 and transform: translateY(100px) and then animate from that state to its default CSS styling.
3.3 gsap.timeline() - Staggering Animations
What if you want to create a sequence of animations? That's where gsap.timeline() comes in. It acts as a container for multiple tweens, allowing you to chain them together and control them as a single unit.
A powerful feature within timelines (and regular tweens) is stagger, which lets you apply animations to multiple elements with a slight delay between each one.
Example: Let's animate three boxes one after another with a stagger effect.
HTML:
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
JavaScript:
gsap.to(".box", {
y: -100,
duration: 1,
stagger: 0.2 // Each box starts animating 0.2s after the previous one
});
This single tween animates all elements with the class .box, but the stagger property creates a beautiful, sequential wave-like effect.
ScrollTrigger In Details
Now, let's level up our animations by making them interactive with the user's scroll position using the ScrollTrigger plugin.
1. What is ScrollTrigger?
ScrollTrigger is a GSAP plugin that lets you trigger animations based on the scroll position. You can make elements animate as they enter the viewport, pin them in place, or even link an animation's progress directly to the scrollbar. It's the secret sauce behind many modern, immersive web experiences.
2. How to Connect ScrollTrigger CDN
ScrollTrigger is a plugin, so it needs to be included in addition to the core GSAP library. The order is important!
<!-- GSAP Core -->
<script src="[https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.5/gsap.min.js](https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.5/gsap.min.js)"></script>
<!-- ScrollTrigger Plugin -->
<script src="[https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.5/ScrollTrigger.min.js](https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.5/ScrollTrigger.min.js)"></script>
<!-- Your JS File -->
<script src="script.js"></script>
You also need to "register" the plugin in your JavaScript file. Just add this line at the top:
gsap.registerPlugin(ScrollTrigger);
3. Working of ScrollTrigger
ScrollTrigger works by adding a scrollTrigger object to your GSAP tweens. This object contains all the configuration for how the animation should behave when scrolling.
3.1 scrollTrigger Object in GSAP
Let's start with a simple example. We'll make a box fade in and scale up as it enters the viewport.
gsap.from(".my-box", {
opacity: 0,
scale: 0.5,
duration: 1,
scrollTrigger: {
trigger: ".my-box", // The element that triggers the animation
start: "top 80%", // When the top of the trigger hits 80% of the viewport height
end: "bottom 20%", // When the bottom of the trigger hits 20% of the viewport height
toggleActions: "play none none reverse" // play on enter, reverse on leave
}
});
3.2 Key scrollTrigger Properties Explained
Let's break down the most important properties you'll use.
triggerDefinition: The element that serves as the trigger for the animation. The animation's start and end points are calculated based on this element's position in the document.
Syntax:
trigger: ".selector"Example:
trigger: "#contact-section"will start the animation when the#contact-sectionenters the viewport.
scrollerDefinition: By default, ScrollTrigger listens to the main browser window's scrollbar. The
scrollerproperty allows you to specify a different scrolling container, like adivwithoverflow: scroll.Syntax:
scroller: ".container"Example: If you have a scrollable panel with the class
.smooth-scroll, you would usescroller: ".smooth-scroll".
markersDefinition: An incredibly useful debugging tool. Setting it to
truedisplays visual markers in the viewport showing thestartandendtrigger points. This is essential for visualizing how your scroll triggers are set up.Syntax:
markers: trueExample:
scrollTrigger: { trigger: ".content", markers: true // Shows start/end markers for debugging }
startandendDefinition: These properties define the exact points at which the animation starts and ends. The value is a string with two keywords. The first refers to a point on the trigger element, and the second refers to a point on the viewport.
Syntax:
start: "trigger-position viewport-position"Common Values:
top,bottom,center, percentages (e.g.,80%).Example 1 (
start):start: "top center"means "when the top of the trigger element reaches the center of the viewport."Example 2 (
end):end: "bottom 20%"means "when the bottom of the trigger element reaches a point 20% from the top of the viewport."
scrubDefinition: This is one of ScrollTrigger's most powerful features. Instead of just playing an animation when triggered,
scrublinks the animation's progress directly to the scrollbar's position. This creates a "scrubbing" effect.Syntax:
scrub: trueorscrub: numberExample 1 (Boolean):
scrub: truecreates a direct, 1-to-1 link between scroll position and animation progress.gsap.to(".logo", { rotation: 360, scrollTrigger: { trigger: ".header", start: "top top", end: "+=500", // End 500px after the start scrub: true, // Link animation to scroll pin: true // Pin the trigger element while scrubbing } });Example 2 (Numeric):
scrub: 1adds a 1-second "smoothing" effect, so the animation lags slightly behind the scroll, creating a much smoother feel.gsap.to(".progress-bar", { scaleX: 1, scrollTrigger: { scrub: 2 // 2 seconds of smoothing } });





