Jump to content
Search Community

How to add mouse wheel scroll to the seamless vertical loop example working with draggable

nvgrd test
Moderator Tag

Go to solution Solved by nvgrd,

Recommended Posts

I'm trying to add the ability to do normal mouse scrolling to this example (). I have tried it with the wheel event but it is very abrupt and does not work well with the draggable. Is there a way to do it with the same behavior and smoothness as the draggable?

 

Thanks in advance!

See the Pen poBRqPW by iacus (@iacus) on CodePen

Link to comment
Share on other sites

Hi @nvgrd and welcome to the GSAP Forums!

 

You can use the Observer Plugin:

https://gsap.com/docs/v3/Plugins/Observer/

 

Also the wrap utility method with the loop's toIndex method should provide basically what you need:

https://gsap.com/docs/v3/GSAP/UtilityMethods/wrap()

 

const boxes = gsap.utils.toArray("#wheel_1 .box").length - 1;
let currentIndex = 0;

const loopUpdater = (e) => {
  const modifier = e.deltaY > 0 ? -2 : 2;
  const target = gsap.utils.wrap(0, boxes, currentIndex + modifier);
  currentIndex = target;
  wheel1.toIndex(target, {duration: 1, revolutions: 0, ease: "power2.inOut"});
};

Observer.create({
  target: ".row",
  type: "wheel",
  onUp: loopUpdater,
  onDown: loopUpdater,
});

Here is a fork of your demo:

See the Pen ZEZXPBR by GreenSock (@GreenSock) on CodePen

 

Hopefully this helps.

Happy Tweening!

Link to comment
Share on other sites

Thanks Rodrigo for the help!

 

Your solution works fine for navigating to some index. But I'm looking for free movement, similar to touch behavior.

 

I was trying to do it with your code but I can't do it. Is it possible to do it with the progress function of the timeline as well as within the draggable to achieve free and loop scrolling at the same time?

 

Thanks!

Link to comment
Share on other sites

Hi,

 

Maybe something like this:

let wheel1 = verticalLoop("#wheel_1 .box", {
  repeat: -1,
  center: true,
  draggable: true, // I'm just being fancy
  inertia: false // even fancier
});
wheel1.timeScale(0);

const boxes = gsap.utils.toArray("#wheel_1 .box").length - 1;
let tl;

const loopUpdater = (e) => {
  tl && tl.kill();
  const modifier = e.deltaY > 0 ? 5 : -5;
  tl = gsap.timeline()
  .to(wheel1, { timeScale: modifier, duration: 0.25, ease: "power1.in" })
  .to(wheel1, { timeScale: 0, duration: 0.5, ease: "power2.out" }, "+=1");
};

Observer.create({
  target: ".row",
  type: "wheel",
  onUp: loopUpdater,
  onDown: loopUpdater
});

See the Pen wvZpGaR by GreenSock (@GreenSock) on CodePen

 

Hopefully this helps.

Happy Tweening!

Link to comment
Share on other sites

  • 2 weeks later...

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...