ScrollTrigger getTween() and getVelocity()

  • Learn how to access ScrollTriggerโ€™s internal tween and measure scroll speed using getTween() and getVelocity().
  • Introduction

    In GreenSock GSAP, ScrollTrigger provides advanced methods to control animations based on scroll.

    Two important helper methods are:

    • getTween() โ†’ access the animation linked to ScrollTrigger

    • getVelocity() โ†’ detect how fast the user is scrolling

    These are commonly used in:

    • Scroll-based animations

    • Momentum effects

    • Speed-based UI reactions

    • Advanced interactive websites

  • Basic Concept

    What is getTween()?

    • Returns the GSAP tween controlled by ScrollTrigger

    • Allows you to pause, play, reverse, or inspect the animation

    What is getVelocity()?

    • Returns the scroll speed

    • Positive value โ†’ scrolling down

    • Negative value โ†’ scrolling up

    ๐Ÿ‘‰ Think of it as a scroll speed meter

getTween()

scrollTrigger.getTween();

getVelocity()

scrollTrigger.getVelocity();
  • Parameters / Options

    MethodWhat it ReturnsNotes
    getTween()GSAP TweenOnly if animation exists
    getVelocity()NumberPixels per second
    Positive valueScroll downโ€”
    Negative valueScroll upโ€”

Using getTween()

ScrollTrigger.create({
  trigger: ".box",
  start: "top center",
  animation: gsap.to(".box", { x: 300 }),
  onEnter(self) {
    const tween = self.getTween();
    tween.pause();
  }
});

Detect Scroll Speed (getVelocity)

ScrollTrigger.create({
  trigger: ".section",
  onUpdate(self) {
    console.log(self.getVelocity());
  }
});

Change Animation Based on Scroll Speed

ScrollTrigger.create({
  onUpdate(self) {
    if (self.getVelocity() > 500) {
      gsap.to(".box", { scale: 1.2 });
    } else {
      gsap.to(".box", { scale: 1 });
    }
  }
});
  • Common Mistakes

    1. โŒ Calling getTween() without animation
      โœ” It works only when ScrollTrigger has an animation

    2. โŒ Using getVelocity() outside callbacks
      โœ” Always use inside onUpdate

    3. โŒ Expecting getVelocity() to return direction text
      โœ” It returns a number, not a string

    4. โŒ Overusing heavy logic inside onUpdate
      โœ” Keep code lightweight for performance

  • Best Practices

    • Use getVelocity() for subtle UI reactions

    • Avoid heavy DOM updates on every scroll

    • Cache values if possible

    • Combine with scrub for smooth effects

  • Comparison

    MethodPurposeUse Case
    getTween()Access animationControl / inspect tween
    getVelocity()Scroll speedMomentum / direction
    self.progressScroll progressTimeline sync
    self.directionScroll directionSimple up/down