Web Development
ScrollTrigger in Depth: Pin, Snap, Callbacks and ScrollTo
ScrollTrigger is the GSAP plugin that ties animations to the scroll: it decides when an animation starts and ends from where an element sits on screen, and it can link the animation's progress to the scrollbar. ScrollToPlugin, its companion, animates the scroll position itself, which is what navigation buttons need.
A product page that explains itself step by step uses all of it at once: one section stays pinned while a shape changes in three stages, the scroll settles exactly on each stage, the feature cards appear as a group, and a menu glides to each part.
This post is part of the GSAP series. The license and the install are in the series hub, matchMedia and context in post 2, on the core, and timelines with labels, which snap relies on here, in post 3. It includes two live demos on GSAP 3.15.0. Three script tags are all it takes:
<script src="https://unpkg.com/gsap@3.15.0/dist/gsap.min.js"></script>
<script src="https://unpkg.com/gsap@3.15.0/dist/ScrollTrigger.min.js"></script>
<script src="https://unpkg.com/gsap@3.15.0/dist/ScrollToPlugin.min.js"></script>
<script>
gsap.registerPlugin(ScrollTrigger, ScrollToPlugin);
</script>
If ScrollToPlugin.min.js is missing, the registerPlugin line throws a ReferenceError and scrollTo moves nothing. In the snippets, .panel, .shape, .card and the other classes stand for elements on the page.
ScrollTrigger: animation that follows the scroll
This is the outline of the first demo, which moves a paper boat and fills a bar while the reader scrolls through it:
gsap.timeline({
scrollTrigger: {
trigger: ".gsapdemo-scroll",
start: "top center",
end: "bottom center",
scrub: 0.5,
markers: true
}
})
.from(".gsapdemo-bar", { scaleX: 0, ease: "none" })
.to(".gsapdemo-shape", { x: 240, rotation: 360, ease: "none" }, "<");
triggeris the element whose position "in the normal document flow" determines where the ScrollTrigger starts and ends.start: "top center"means "when the top of the trigger hits the center of the scroller", and the scroller is the viewport by default. Withoutpin, the default is"top bottom".enduses the same format and defaults to"bottom top".scrub: truelinks progress directly to the scrollbar. With a number,scrub: 0.5, the playhead takes 0.5 seconds to catch up, which smooths the motion.markers: truedraws the start and end points on the page for debugging. It has to go before shipping.
Both steps use ease: "none" because with scrub the reader sets the pace; an ease on top would make the bar drift away from the scroll position. The "<" makes the shape and the bar start together.
If the page scrolls inside a container rather than the window, ScrollTrigger needs the scroller option, as a selector or the element itself. This blog works that way. Without it, ScrollTrigger keeps listening to the window, which never scrolls, so the animation never moves. If the selector matches nothing, the call throws a TypeError and no ScrollTrigger is created:
gsap.to(".item", {
x: 300,
scrollTrigger: {
trigger: ".item",
scroller: ".scroll-container",
start: "top center",
end: "bottom center",
scrub: 0.5
}
});
Pinning an element on screen with pin gets its own section further down, next to the second demo.
0 % · scroll: the boat and the bar follow you.
When CSS is enough
CSS can already drive animations from the scroll position with animation-timeline and its scroll() and view() functions: Chrome and Edge since 115, Safari since 26 and Firefox since 159, released on October 27, 2026. Support is recent: a project that serves older browsers needs a fallback (a regular time-based CSS animation, or ScrollTrigger), and the scroll-driven version can sit inside @supports (animation-timeline: scroll()). The result gets tested against the project's browser list, not just the development one. If all that is needed is something that progresses with the scroll, CSS does it. ScrollTrigger adds start and end relative to an element, a scrub the docs call "great for smoothing things out", debug markers, a whole timeline driven by the scroll, and everything below: callbacks, pin, snap and batch.
toggleActions and callbacks
Without scrub, the animation does not track the scroll; it reacts at four moments. toggleActions sets what happens at each, "onEnter, onLeave, onEnterBack, and onLeaveBack, in that order", and the default is "play none none none". The valid keywords are play, pause, resume, reset, restart, complete, reverse and none; a misspelled one, such as resum, is ignored with no warning.
gsap.to(".notice", {
x: 200,
scrollTrigger: {
trigger: ".notice",
toggleActions: "play pause resume reset",
onToggle: (self) => console.log(self.isActive ? "active" : "inactive"),
onUpdate: (self) => console.log(self.progress.toFixed(2), self.direction)
}
});
With those values the animation plays on enter, pauses on leave, resumes when the scroll comes back in and resets once it goes back past the start. resume is not reverse: resume carries on from where it paused, in the direction it was going, and reverse plays it backwards. With toggleActions: "play pause reverse reset", scrolling back in from below undoes the animation instead of finishing it. Every callback receives the instance, with progress, direction, isActive and getVelocity(). onToggle can replace all four by checking isActive, with one documented catch: if the scroll shoots past both start and end in the same tick, it does not fire. And with a numeric scrub, the animation keeps moving briefly after the scroll stops; to react to every frame of the animation, onUpdate goes on the animation rather than on the ScrollTrigger.
pin: holding a section in place
With pin, an element appears to "stick" in its starting position while the rest of the content keeps scrolling. Only one element can be pinned, though it may hold as many children as needed, and pin: true pins the trigger itself. end: "+=800" sets how much scrolling it stays pinned for.
gsap.timeline({
scrollTrigger: {
trigger: ".panel",
pin: true,
start: "top top",
end: "+=800",
scrub: 0.5
}
})
.to(".panel .shape", { x: 200 })
.to(".panel .shape", { rotation: 180 });
By default, pinSpacing adds padding below the pinned element to push the following content down, so that once it is released the content "catches up perfectly". Without it, the content that follows would slide under the pinned element. Depending on the scroller, with the window (the <body>) the pin uses position: fixed, and with any other container it uses transforms, "because position: fixed won't work in various nested scenarios". One more case: if the pinned element's parent is display: flex, ScrollTrigger defaults to pinSpacing: false and does not reserve the 800px. To reserve them, pinSpacing: true goes in explicitly, and the flex container's layout is worth checking afterwards.
snap: settling on labels
snap moves progress to set values "after the user stops scrolling". It takes a number (for N sections, 1 / (N - 1)), an array of values, a function, "labels", which lands on the closest label in the timeline, and "labelsDirectional", which picks the closest one in the direction of the last scroll. The labels are the ones from post 3.
gsap.timeline({
scrollTrigger: {
trigger: ".panel",
pin: true,
start: "top top",
end: "+=800",
scrub: 0.5,
snap: { snapTo: "labels", duration: 0.3, delay: 0.1, ease: "power1.inOut" }
}
})
.addLabel("start")
.to(".panel .shape", { x: 200 })
.addLabel("middle")
.to(".panel .shape", { rotation: 180 })
.addLabel("end");
In the object form only snapTo is required. The snap ease defaults to "power3" (eases are covered in post 4), and delay to half the scrub value. Since 3.8.0, a number or an array snaps directionally by default, heading the way the reader last scrolled. "labels" goes to the closest label even if it lies behind, and that is what the demo uses: with the scroll stopped at 0.38, progress ends at 0.5. With no labels in the timeline, "labels" snaps to nothing and gives no warning.
ScrollTrigger.batch(): as a group
batch() creates one ScrollTrigger per element and batches their callbacks, so each callback gets an array of every element that crossed at about the same time. That makes a stagger like the one in post 2 trivial.
gsap.set(".card", { opacity: 0 });
ScrollTrigger.batch(".card", {
start: "top 90%",
onEnter: (els) => gsap.to(els, { opacity: 1, stagger: 0.1 })
});
The docs pitch it as an alternative to IntersectionObserver that is "more widely compatible and easier to work with". Like any ScrollTrigger, inside a scrolling container it needs scroller; without it, the cards stay at opacity: 0.
ScrollToPlugin: animated navigation
ScrollToPlugin "Animates the scroll position of the window" or of an element; for a div, the div needs scrollable overflow. offsetY leaves a gap at the top, and autoKill: true cancels the tween when the reader grabs the scroll halfway. If the target does not exist, GSAP warns "scrollTo target doesn't exist. Using 0" and scrolls to the very top.
// the window, as in the official example
gsap.to(window, { duration: 2, scrollTo: "#section" });
// a box with overflow-y: auto that contains #cards
gsap.to(".box", { duration: 0.6, scrollTo: { y: "#cards", offsetY: 20, autoKill: true } });
The docs warn that using it alongside scroll-behavior: smooth in CSS "will cause conflicts", since the browser and GSAP fight over the same scroll. That is why the box in the second demo declares scroll-behavior: auto.
The demo: a box with its own scroll
The second demo puts it all together: a section pinned for 800px of scrolling with a three-label timeline, snapping to those labels, four cards entering through batch, a status line driven by onToggle, and three ScrollTo buttons. It lives inside a box with its own scroll, passed as the scroller, so nothing gets pinned over the post's column. Under reduced motion there is no pin or snap: the buttons jump without animating and Play animation runs the scene once.
1. Scroll inside this box
The next section stays pinned while you scroll, and when you stop it snaps to the nearest label.
2. Pinned section
3. Cards as a group
All four enter together through a single ScrollTrigger.batch() onEnter.
4. End
Use the buttons above to go back.
Pinned section: inactive · label: start
Common mistakes
- Stale scroll positions.
startandendare calculated when the ScrollTrigger is created and when the scroller is resized, not on every scroll. After a DOM change, ScrollTrigger.refresh() recalculates them. - ScrollTriggers on tweens nested in a timeline. The ScrollTrigger mistakes page sums it up: "Logic-wise, that can't work." A single one goes on the timeline.
- Creating ScrollTriggers out of order around a pin. With
pinSpacing, the ones that come later "need to compensate for the extra distance that the pinning adds". They are created in page order. - One tween for several sections. It "ends up animating them all at once": with three
.itemelements, the scroller block creates a single ScrollTrigger and all three move together. The fix is one per section, orbatch. - ScrollTo on a container with
scroll-behavior: smooth. Both animate the scroll at the same time; that container goes toauto.
FAQ
ScrollTrigger or CSS animation-timeline?
For progress tied to the scroll, animation-timeline does it without JavaScript in recent versions of all four major browsers. For pin, snap, callbacks, batch or a full timeline, ScrollTrigger.
Why does the demo use a box with its own scroll?
So the pin never sticks anything over the post's text. On a normal page the scroller is usually the window and everything works the same, except that the pin uses position: fixed.
Conclusion
The basics are in the series hub, post 2, post 3 and post 4. Next up is post 6, on text animation; gestures without a scrollbar (wheel, touch and pointer) come with Observer in post 9.
References
- GSAP — ScrollTrigger
- GSAP — ScrollTrigger.batch()
- GSAP — ScrollTrigger.refresh()
- GSAP — ScrollTrigger common mistakes
- GSAP — ScrollToPlugin
- MDN — animation-timeline
- Mozilla — Firefox 159 release notes
Tags
Share this post
Subscribe
Get the latest posts delivered right to your inbox.
Leave a comment