GSAP Motion

GSAP ScrollTrigger Performance Without the Jank

GSAP ScrollTrigger performance is mostly about how much work you ask the browser to do on every scroll frame. In my builds I batch similar triggers, set once: true for one-shot reveals, refresh only after layout actually changes, reserve pinning for a few deliberate sections, and never read layout inside a scroll loop. The result is motion that holds frame rate.

By Saif Al Jahanwebdev-saif.com

Where ScrollTrigger time actually goes

ScrollTrigger is cheap when it is used as a thin event layer that flips a class or plays a tween. It gets expensive for three reasons: too many separate trigger instances each running their own scroll logic, continuous scrub tweens that write styles on every frame, and layout reads that force the browser to recalculate geometry mid-scroll. A pinned section adds a fourth cost because it changes document flow and reserves spacer space. I treat the scroll handler as a hot path: no DOM queries, no getBoundingClientRect, no offsetHeight reads while the user is moving. Everything that can be measured once at setup or on a controlled refresh gets measured there. That single habit removes more jank than any micro-optimisation.

Batch triggers instead of creating one per element

The most common performance mistake I see is a loop that creates one ScrollTrigger per card, with each trigger firing its own tween and its own onEnter callback. ScrollTrigger.batch() collapses that into a small number of shared triggers that fire a callback with an array of elements. You then call gsap.to() on the whole array, so the engine builds one tween instead of dozens. The batch accepts interval and batchMax, which let you control how many elements are grouped and how quickly batches fire during a fast scroll. For grids and lists with 20 to 200 items, this is the single biggest win.

  • Use ScrollTrigger.batch(targets, { onEnter: batch => gsap.to(batch, {...}) })
  • Set batchMax to a sensible group size, for example 6 to 12 elements
  • Use interval to throttle callbacks during fast scrolling
  • Add once: true inside the batch config for reveal-only effects
  • Prefer CSS classes plus a single tween over per-element inline writes

Use once: true for reveals and keep scrub deliberate

If an animation plays a single time when an element enters the viewport, there is no reason for ScrollTrigger to keep evaluating it on the way back out. Setting once: true kills the trigger after its first onEnter, which permanently removes that work from every later scroll. I use it for section reveals, counters, image fades and split-text entrances. Scrub is the opposite case: it ties tween progress to scroll position, so it runs continuously while the section is active. That is fine for one hero or one pinned narrative, but it should not be applied to every block on the page. When I do need scrub, I keep the tween on transform and opacity, avoid animating layout properties, and sometimes add a small smoothing value so rapid scroll changes do not cause visible stutter.

  • once: true for enter-only reveals, counters and fade-ins
  • scrub only on one or two focal sections per page
  • Animate transform and opacity, not width, height or top
  • Use a numeric scrub value for light smoothing when needed
  • Avoid toggling reveal animations back and forth on every pass

Refresh discipline: call refresh only when layout really changes

ScrollTrigger.refresh() recalculates every start and end position on the page. It is necessary after content that changes document height: loading images without reserved dimensions, injecting markup, opening an accordion, or switching tabs. It is wasteful when called on every resize event, on every scroll, or after a tween that does not affect layout. On mobile I enable ScrollTrigger.config({ ignoreMobileResize: true }) so the browser chrome hiding and showing on scroll does not trigger a cascade of refreshes. I also set anticipatePin: 1 on pinned triggers to reduce the chance of a jump, and invalidateOnRefresh: true where start and end values are computed from measured sizes. The rule is simple: fix the layout first, then refresh once.

  • Give images explicit width and height so they do not resize the page
  • Call refresh() after DOM or height changes, not on a timer
  • Use ScrollTrigger.config({ ignoreMobileResize: true }) on mobile
  • Set invalidateOnRefresh: true for measured start and end values
  • Wrap dynamic content updates, then refresh once at the end

Pinning has a real cost — budget it

Pinning is the most expensive ScrollTrigger feature because it changes the document flow: ScrollTrigger inserts a pin-spacer, fixes the element in place, and tracks progress over a long scroll distance. One pinned hero or one pinned step-by-step section is usually a good trade. Pinning five sections, nesting pins, or pinning elements inside a CSS transform or an overflow container is where pages start to stutter and mis-measure. I keep pin durations short, avoid pinning inside flex or grid children that reflow, and check pinSpacing carefully so the spacer does not add unexpected height. I also test with markers enabled during development, then remove them before launch. Where a sticky effect would do the same job visually, I use CSS position: sticky instead of a pinned trigger.

  • Limit most pages to one or two pinned sections
  • Prefer CSS position: sticky for simple hold-in-place effects
  • Avoid pinning elements inside transformed or overflow-hidden parents
  • Keep pin distances short and verify pinSpacing behaviour
  • Use markers in development only, never in production

Never read layout inside a scroll loop

Layout thrashing is the classic ScrollTrigger performance trap. A callback reads element.offsetTop, then writes a style, then another callback reads getBoundingClientRect(), forcing the browser to flush pending style and layout work again on every frame. The fix is to measure once and cache. I capture sizes and positions during setup or inside a controlled refresh, store them in variables, and let the scroll callback only apply transforms or classes. If I must respond to an element changing size, I use a ResizeObserver and refresh on a debounced schedule rather than polling on scroll. For touch and wheel responsiveness I also keep the main thread clear: no heavy string work, no JSON parsing, and no synchronous network calls in onUpdate.

How I verify it in a WordPress or PHP build

On webdev-saif.com projects I register animations through gsap.context(), which keeps triggers scoped to a component and makes cleanup reliable when WordPress blocks re-render or an Elementor widget is swapped. That matters because orphaned triggers from a removed widget keep running and quietly eat frame time. I scope selectors to a root element, revert the context on teardown, and use gsap.matchMedia() so reduced-motion users and small screens get a simpler, cheaper animation path. Verification is practical rather than theoretical: I record a scroll session in Chrome DevTools with the Performance panel, look for long frames and forced reflows, then check the Core Web Vitals report for INP regressions. I write this kind of motion into builds from Mymensingh, Bangladesh for clients who need the effect without the slowdown.

Frequently asked questions

Is ScrollTrigger.batch() actually faster than a trigger per element?

Yes, for repeated elements. Instead of dozens of independent triggers each evaluating scroll position and starting its own tween, batch groups them into a few shared triggers and one tween per batch. The gain grows with element count: it is noticeable in a 12-card grid and substantial in a 100-item list.

When should I use once: true in ScrollTrigger?

Use it for any animation that should play only the first time an element enters view: reveals, counters, fade-ins and split-text intros. The trigger is killed after that first entry, so it stops consuming scroll work. Do not use it when you deliberately want the effect to replay on every visit.

How often should I call ScrollTrigger.refresh()?

Only when document layout changes. Call it after images load without reserved dimensions, after content is injected, or after an accordion or tab changes height. Avoid calling it on every scroll or every resize. On mobile, enable ignoreMobileResize so browser chrome resizing does not cause repeated refreshes.

Why does pinning slow my page down?

Pinning inserts a pin-spacer and changes document flow, so ScrollTrigger must track progress over a long distance and keep the element fixed. One or two pinned sections are fine. Many pins, nested pins, or pins inside transformed or overflow containers cause extra recalculation and visible jumps.

What is layout thrashing and how do I avoid it in scroll animations?

It is the repeated read-write-read pattern where a callback reads geometry, writes a style, then another read forces the browser to recompute layout again. Avoid it by measuring sizes and positions once during setup or refresh, caching those values, and letting scroll callbacks only write transforms or classes.

Written by Saif Al Jahan, webdev-saif.com · Mymensingh, Bangladesh. Last updated .