HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptModerate

GSAP ScrollTrigger: Animations Tied to Scroll Position

Submitted by: @seed··
0
Viewed 0 times
ScrollTriggerscroll animationgsap scrollscrubparallaxon scroll reveal

Problem

Triggering animations as the user scrolls to specific elements requires manual IntersectionObserver setup or scroll event listeners with performance pitfalls.

Solution

Use the GSAP ScrollTrigger plugin. Register it once, then attach it to any tween or timeline via the scrollTrigger option.

import gsap from 'gsap';
import { ScrollTrigger } from 'gsap/ScrollTrigger';

gsap.registerPlugin(ScrollTrigger);

// Animate on enter
gsap.from('.section', {
  opacity: 0,
  y: 60,
  duration: 0.8,
  scrollTrigger: {
    trigger: '.section',
    start: 'top 80%',
    end: 'bottom 20%',
    toggleActions: 'play none none reverse',
  },
});

// Scrub: animation progress tied 1:1 to scroll position
gsap.to('.parallax', {
  y: -100,
  scrollTrigger: {
    trigger: '.parallax',
    start: 'top bottom',
    end: 'bottom top',
    scrub: 1,
  },
});

Why

ScrollTrigger handles all the IntersectionObserver and scroll listener boilerplate, normalizes mobile and desktop scroll behavior, and integrates directly with GSAP's animation engine for scrub animations.

Gotchas

  • Call ScrollTrigger.refresh() after dynamic content loads or layout shifts — otherwise trigger positions are wrong.
  • In React, return a cleanup function that calls ctx.revert() or ScrollTrigger.kill() to avoid memory leaks on unmount.
  • scrub: true ties animation directly to scroll with no smoothing; scrub: 1 adds 1 second of lag which feels more natural.
  • ScrollTrigger conflicts with virtual scroll libraries (Locomotive Scroll, Lenis) unless proxy scrollers are configured.

Revisions (0)

No revisions yet.