Manipulating GSAP's own tweens — tweening a tween's playhead rather than just elements on screen — is how this technique achieves a seamless infinite scroll with no duplicated cards. ScrollTrigger is layered on top to scrub the result, and utilities like wrap and snap handle the loop and snapping logic.

Tweening the playhead

GSAP is usually reached for to animate visible things, but it can animate almost anything — including a timeline's position in time. Think of a cartoon: a collection of compositions, each a scene, scrubbed with a remote. That's three layers, and that layering is what enables different kinds of infinite loops.

Start with ten boxes moved left to right using fromTo with repeat and stagger. There's a gap at the start of each iteration.

gsap.fromTo('.box', {
  xPercent: 100
}, {
  xPercent: -200,
  stagger: 0.5,
  duration: 1,
  repeat: -1,
  ease: 'none',
})

The meta move: pause that tween, keep it in a variable, and create a second tween that plays it by animating its totalTime. That property gets or sets the playhead while accounting for repeats and repeat delays.

const SHIFT = gsap.fromTo('.box', {
  xPercent: 100
}, {
  paused: true,
  xPercent: -200,
  stagger: 0.5,
  duration: 1,
  repeat: -1,
  ease: 'none',
})

const DURATION = SHIFT.duration()

gsap.to(SHIFT, {
  totalTime: DURATION,
  repeat: -1,
  duration: DURATION,
  ease: 'none',
})

The output looks identical, but now there's an extra control layer. Changing the easing on this layer (say to power4.in) changes the animation without touching the underlying tween. You can also repeat only a slice of the timeline by tweaking the position values passed to a nested fromTo:

gsap.fromTo(SHIFT, {
  totalTime: 2,
}, {
  totalTime: DURATION - 1,
  repeat: -1,
  duration: DURATION,
  ease: 'none'
})

The loop keeps running, yet the numbers flip on each repeat while the boxes stay correctly placed.

Building a seamless window

A gap remains between repetitions. Fix this by creating the tween via a function and adding it to a timeline three times — equivalent to repeat: 3, with repeat: -1 removed from the original.

const getShift = () => gsap.fromTo('.box', {
  xPercent: 100
}, {
  xPercent: -200,
  stagger: 0.5,
  duration: 1,
  ease: 'none',
})

const LOOP = gsap.timeline()
  .add(getShift())
  .add(getShift())
  .add(getShift())

Use the position parameter when adding those tweens so each set is inserted before the previous one ends — a value derived from stagger and the element count.

const stagger = 0.5 // Used in our shifting tween
const BOXES = gsap.utils.toArray('.box')
const LOOP = gsap.timeline({
  repeat: -1
})
  .add(getShift(), 0)
  .add(getShift(), BOXES.length * stagger)
  .add(getShift(), BOXES.length * stagger * 2)

There is now a window in the middle that loops seamlessly. Tweening totalTime across that window is the next step:

const LOOP = gsap.timeline({
  paused: true,
  repeat: -1,
})
.add(getShift(), 0)
.add(getShift(), BOXES.length * stagger)
.add(getShift(), BOXES.length * stagger * 2)

gsap.fromTo(LOOP, {
  totalTime: 4.75,
},
{
  totalTime: '+=5',
  duration: 10,
  ease: 'none',
  repeat: -1,
})

This tweens totalTime from 4.75 by the length of a cycle (5) using GSAP's += syntax. A watch demo (hands completing a revolution in 12 seconds, looped with repeat: -1) helps visualize this: narrowing the window to, say, 2–6 with a duration of 1 makes the hands run from 2 o'clock to 6 o'clock repeatedly, without changing the underlying animation.

const DURATION = 1
const CYCLE_DURATION = BOXES.length * STAGGER
const START_TIME = CYCLE_DURATION + (DURATION * 0.5)
const END_TIME = START_TIME + CYCLE_DURATION

Rather than stacking three timelines, loop over the elements three times to build one big timeline — no position math required:

const STAGGER = 0.5
const BOXES = gsap.utils.toArray('.box')

const LOOP = gsap.timeline({
  paused: true,
  repeat: -1,
})

const SHIFTS = [...BOXES, ...BOXES, ...BOXES]

SHIFTS.forEach((BOX, index) => {
  LOOP.fromTo(BOX, {
    xPercent: 100
  }, {
    xPercent: -200,
    duration: 1,
    ease: 'none',
  }, index * STAGGER)
})

Adjusting stagger squishes the boxes closer and breaks the window, since totalTime shifts. Plug in the formula above and it is fixed again; an offset value changes the starting position (though it leaves awkward stacks at each end).

const DURATION = 1
const CYCLE_DURATION = STAGGER * BOXES.length
const START_TIME = CYCLE_DURATION + (DURATION * 0.5)
const END_TIME = START_TIME + CYCLE_DURATION

gsap.fromTo(LOOP, {
  totalTime: START_TIME,
},
{
  totalTime: END_TIME,
  duration: 10,
  ease: 'none',
  repeat: -1,
})
const STAGGER = 0.5
const OFFSET = 5 * STAGGER
const START_TIME = (CYCLE_DURATION + (STAGGER * 0.5)) + OFFSET

Entering and exiting

Those end stacks are solved by treating document.body as a physical window: boxes scale up on enter and down on exit, using yoyo with repeat: 1 inside individual timelines per box.

SHIFTS.forEach((BOX, index) => {
  const BOX_TL = gsap
    .timeline()
    .fromTo(
      BOX,
      {
        xPercent: 100,
      },
      {
        xPercent: -200,
        duration: 1,
        ease: 'none',
      }, 0
    )
    .fromTo(
      BOX,
      {
        scale: 0,
      },
      {
        scale: 1,
        repeat: 1,
        yoyo: true,
        ease: 'none',
        duration: 0.5,
      },
      0
    )
  LOOP.add(BOX_TL, index * STAGGER)
})

A timeline duration of 1 keeps things simple — the box is at its midpoint at 0.5. Note easing behaves differently here: an ease-in would bunch the boxes on the right before they move across. The boxes also vanish briefly in the middle; adding immediateRender (the equivalent of animation-fill-mode: none) tells GSAP not to retain or pre-record set styles.

SHIFTS.forEach((BOX, index) => {
  const BOX_TL = gsap
    .timeline()
    .fromTo(
      BOX,
      {
        xPercent: 100,
      },
      {
        xPercent: -200,
        duration: 1,
        ease: 'none',
        immediateRender: false,
      }, 0
    )
    .fromTo(
      BOX,
      {
        scale: 0,
      },
      {
        scale: 1,
        repeat: 1,
        zIndex: BOXES.length + 1,
        yoyo: true,
        ease: 'none',
        duration: 0.5,
        immediateRender: false,
      },
      0
    )
  LOOP.add(BOX_TL, index * STAGGER)
})

That fixes it. Setting z-index: BOXES.length guards against stacking issues. To show more boxes at once, use STAGGER of 0.2 and add opacity; repeatDelay lets the fade complete in 0.25 seconds, hold for 0.5, then fade out over 0.25. The loop window remains the key ingredient.

.fromTo(
  BOX, {
    opacity: 0,
  }, {
    opacity: 1,
    duration: 0.25,
    repeat: 1,
    repeatDelay: 0.5,
    immediateRender: false,
    ease: 'none',
    yoyo: true,
  }, 0)

Wiring it to scroll

ScrollTrigger scrubs the loop window via an extra tween, with the loop now paused. The boxes are wrapped in a .boxes element pinned to the viewport while vertical scrolling scrubs the playhead by updating totalTime of SCRUB:

const LOOP_HEAD = gsap.fromTo(LOOP, {
  totalTime: START_TIME,
},
{
  totalTime: END_TIME,
  duration: 10,
  ease: 'none',
  repeat: -1,
  paused: true,
})

const SCRUB = gsap.to(LOOP_HEAD, {
  totalTime: 0,
  paused: true,
  duration: 1,
  ease: 'none',
})
import ScrollTrigger from 'https://cdn.skypack.dev/gsap/ScrollTrigger'
gsap.registerPlugin(ScrollTrigger)

ScrollTrigger.create({
  start: 0,
  end: '+=2000',
  horizontal: false,
  pin: '.boxes',
  onUpdate: self => {
    SCRUB.vars.totalTime = LOOP_HEAD.duration() * self.progress
    SCRUB.invalidate().restart()
  }
})

Inside onUpdate is the core: the tween's totalTime is set from scroll progress; invalidate flushes recorded positions and restart applies the new totalTime. The result scrubs a timeline that scrubs a window of a timeline — in both directions.

Infinite scroll via a proxy

Instead of scrubbing LOOP_HEAD.totalTime directly, update it through a proxy object holding the playhead position:

const PLAYHEAD = { position: 0 }

SCRUB now updates position, and GSAP's wrap utility wraps that value around the LOOP_HEAD duration (duration 10, value 11, result 1). ScrollTrigger must then drive position rather than totalTime.

const POSITION_WRAP = gsap.utils.wrap(0, LOOP_HEAD.duration())

const SCRUB = gsap.to(PLAYHEAD, {
  position: 0,
  onUpdate: () => {
    LOOP_HEAD.totalTime(POSITION_WRAP(PLAYHEAD.position))
  },
  paused: true,
  duration: 1,
  ease: 'none',
})
ScrollTrigger.create({
  start: 0,
  end: '+=2000',
  horizontal: false,
  pin: '.boxes',
  onUpdate: self => {
    SCRUB.vars.position = LOOP_HEAD.duration() * self.progress
    SCRUB.invalidate().restart()
  }
})

The obvious idea — scroll back to the start when progress completes — makes the playhead scrub backwards, which is wrong. totalTime solves this: had the loop head duration passed 5, it never scrubs back to 0 but continues to 10, then 15, and so on. An iteration variable tracks scrub position and only updates at the progress thresholds.

let iteration = 0
const TRIGGER = ScrollTrigger.create({
  start: 0,
  end: '+=2000',
  horizontal: false,
  pin: '.boxes',
  onUpdate: self => {
    const SCROLL = self.scroll()
    if (SCROLL > self.end - 1) {
      // Go forwards in time
      WRAP(1, 1)
    } else if (SCROLL < 1 && self.direction <; 0) {
      // Go backwards in time
      WRAP(-1, self.end - 1)
    } else {
      SCRUB.vars.position = (iteration + self.progress) * LOOP_HEAD.duration()
      SCRUB.invalidate().restart() 
    }
  }
})

iteration now factors into the position calculation (later wrapped), and hitting the scroll limits triggers the WRAP function, which sets the appropriate iteration and new scroll position:

const WRAP = (iterationDelta, scrollTo) => {
  iteration += iterationDelta
  TRIGGER.scroll(scrollTo)
  TRIGGER.update()
}

Adding snap

gsap.utils.snap needs no extra dependencies: declare the step between 0 and 1 — with 10 boxes, 0.1 works — and it returns a function for snapping position. Snapping should only occur after scrolling ends, via a ScrollTrigger event listener:

const SNAP = gsap.utils.snap(1 / BOXES.length)
ScrollTrigger.addEventListener('scrollEnd', () => {
  scrollToPosition(SCRUB.vars.position)
})

The scrollToPosition function handles three steps: calculate the snap point in time; calculate current progress (loop duration 1, snapped to 2.5, gives progress 0.5 and iteration 2, where 2.5 - 1 * 2 / 1 === 0.5), keeping it between 1 and 0; and calculate the scroll destination as a fraction of the 2000 scroll distance, mapped by progressToScroll. A clamp ensures the value is never 0 or 2000 — snapping there would cause an infinite loop.

const scrollToPosition = position => {
  const SNAP_POS = SNAP(position)
  const PROGRESS =
    (SNAP_POS - LOOP_HEAD.duration() * iteration) / LOOP_HEAD.duration()
  const SCROLL = progressToScroll(PROGRESS)
  TRIGGER.scroll(SCROLL)
}
const progressToScroll = progress =>
  gsap.utils.clamp(1, TRIGGER.end - 1, gsap.utils.wrap(0, 1, progress) * TRIGGER.end)

Snappiness comes from a shorter scratch duration and a punchier ease. One flaw remains: wrapping inside a snap can make the playhead jump, so wrapping must also happen on snap — but only when necessary.

const SCRUB = gsap.to(PLAYHEAD, {
  position: 0,
  onUpdate: () => {
    LOOP_HEAD.totalTime(POSITION_WRAP(PLAYHEAD.position))
  },
  paused: true,
  duration: 0.25,
  ease: 'power3',
})
const scrollToPosition = position => {
  const SNAP_POS = SNAP(position)
  const PROGRESS =
    (SNAP_POS - LOOP_HEAD.duration() * iteration) / LOOP_HEAD.duration()
  const SCROLL = progressToScroll(PROGRESS)
  if (PROGRESS >= 1 || PROGRESS < 0) return WRAP(Math.floor(PROGRESS), SCROLL)
  TRIGGER.scroll(SCROLL)
}

Extensions

All that's left is manipulating time further: Next and Previous buttons or keyboard controls hook up by reusing scrollToPosition and bumping its value.

const NEXT = () => scrollToPosition(SCRUB.vars.position - (1 / BOXES.length))
const PREV = () => scrollToPosition(SCRUB.vars.position + (1 / BOXES.length))

// Left and Right arrow plus A and D
document.addEventListener('keydown', event => {
  if (event.keyCode === 37 || event.keyCode === 65) NEXT()
  if (event.keyCode === 39 || event.keyCode === 68) PREV()
})

document.querySelector('.next').addEventListener('click', NEXT)
document.querySelector('.prev').addEventListener('click', PREV)

The takeaways: GSAP can animate an animation; timing acts as a positioning tool; ScrollTrigger can scrub through a proxy; and GSAP utilities handle the surrounding logic. No duplicate elements are needed, and the same meta approach could extend to audio, video, or other scrub targets.