Jump to content
Search Community

Draggable carousel works improperly if I set repeat: -1

PosteProgrammer test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

There is no issue if Inertia Plugin is not there, you can remove the intertia: true config in the loop function and the check for the Inertia Plugin in order to not have that warning:

// Remove this line
typeof(InertiaPlugin) === "undefined" && console.warn("InertiaPlugin required for momentum-based scrolling and snapping. https://greensock.com/club");

draggable = Draggable.create(proxy, {
  trigger: items[0].parentNode,
  type: "x",
  onPressInit() {
    let x = this.x;
    gsap.killTweensOf(tl);
    startProgress = tl.progress();
    refresh();
    ratio = 1 / totalWidth;
    initChangeX = (startProgress / -ratio) - x;
    gsap.set(proxy, {x: startProgress / -ratio});
  },
  onDrag: align,
  onThrowUpdate: align,
  overshootTolerance: 0,
  // REMOVE THIS LINE
  inertia: true,
  // REMOVE THE SNAP CALLBACK AS WELL
  snap(value) {
    //note: if the user presses and releases in the middle of a throw, due to the sudden correction of proxy.x in the onPressInit(), the velocity could be very large, throwing off the snap. So sense that condition and adjust for it. We also need to set overshootTolerance to 0 to prevent the inertia from causing it to shoot past and come back
    if (Math.abs(startProgress / -ratio - this.x) < 10) {
      return lastSnap + initChangeX
    }
    let time = -(value * ratio) * tl.duration(),
        wrappedTime = timeWrap(time),
        snapTime = times[getClosest(times, wrappedTime, tl.duration())],
        dif = snapTime - wrappedTime;
    Math.abs(dif) > tl.duration() / 2 && (dif += dif < 0 ? tl.duration() : -tl.duration());
    lastSnap = (time + dif) / tl.duration() / -ratio;
    return lastSnap;
  },
  onRelease() {
    syncIndex();
    draggable.isThrowing && (indexIsDirty = true);
  },
  onThrowComplete: syncIndex
})[0];

That should work as expected with Draggable but without the Inertia Plugin.

 

Hopefully this helps.

Happy Tweening!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...