ScrollTrigger .direction Property
-
Learn how to detect scroll up and scroll down using ScrollTrigger
.directionand create direction-based animations
Introduction
In many modern websites, animations behave differently when scrolling up or down.
GSAP ScrollTrigger provides a
.directionproperty to detect:-
⬇️ Scrolling down
-
⬆️ Scrolling up
This helps you:
-
Hide headers on scroll down
-
Show headers on scroll up
-
Play different animations based on scroll direction
-
Basic Concept of
.directionThe
.directionproperty tells which way the user is scrolling.Value Meaning 1Scrolling DOWN -1Scrolling UP 📌
.directionis accessed inside ScrollTrigger callbacks likeonUpdate.
Basic Syntax
ScrollTrigger.create({
trigger: ".section",
onUpdate: (self) => {
console.log(self.direction);
}
});
What happens here?
-
self.directiongives scroll direction -
Logs
1or-1while scrolling
-
Detect Scroll Direction
ScrollTrigger.create({
onUpdate: (self) => {
if (self.direction === 1) {
console.log("Scrolling Down");
} else {
console.log("Scrolling Up");
}
}
});
Hide Header on Scroll Down
ScrollTrigger.create({
start: 0,
end: "max",
onUpdate: (self) => {
if (self.direction === 1) {
gsap.to(".header", { y: -100 });
} else {
gsap.to(".header", { y: 0 });
}
}
});
Change Animation Direction
gsap.to(".box", {
x: 300,
scrollTrigger: {
trigger: ".box",
onUpdate: (self) => {
gsap.to(".box", {
rotation: self.direction === 1 ? 360 : 0
});
}
}
});
Parameters Related to Direction
Property Description onUpdate Fires on every scroll update self.direction Returns scroll direction start / end Control trigger range scrub Sync animation with scroll
Common Mistakes
-
❌ Using
.directionoutside ScrollTrigger -
❌ Expecting
directioninside normal GSAP animation -
❌ Forgetting
ScrollTrigger.create() -
❌ Heavy animations inside
onUpdate(performance issue) -
❌ Not limiting trigger range
-