ScrollTrigger .isActive Property
-
Learn how to check if a ScrollTrigger is currently active using
.isActiveand build smart scroll-based UI behaviors.
Introduction
Sometimes you need to know whether a section is currently active on scroll or not.
GSAP ScrollTrigger provides the
.isActiveproperty to check if the scroll position is betweenstartandend.Why
.isActiveis useful-
Highlight active sections
-
Trigger UI changes only when section is visible
-
Control navigation, headers, or indicators
-
What is
.isActive?.isActiveis a boolean value.Value Meaning trueScrollTrigger is active falseScrollTrigger is not active
- A ScrollTrigger becomes active after
startand beforeend.
Basic Syntax
ScrollTrigger.create({
trigger: ".section",
start: "top center",
end: "bottom center",
onUpdate: (self) => {
console.log(self.isActive);
}
});
Detect Active Section
ScrollTrigger.create({
trigger: ".box",
start: "top 80%",
end: "bottom 20%",
onUpdate: (self) => {
if (self.isActive) {
console.log("Box is active");
}
}
});
Add Class When Section is Active
ScrollTrigger.create({
trigger: ".section",
onUpdate: (self) => {
document
.querySelector(".section")
.classList.toggle("active", self.isActive);
}
});
Change Navbar Based on Active Section
ScrollTrigger.create({
trigger: ".hero",
start: "top top",
end: "bottom top",
onUpdate: (self) => {
gsap.to(".navbar", {
backgroundColor: self.isActive ? "#000" : "transparent"
});
}
});
Where
.isActiveWorks BestUse Case Example Section highlight Active menu item UI changes Navbar style Animations Play only when active Logic checks Conditional triggers