locomotive-scroll icon indicating copy to clipboard operation
locomotive-scroll copied to clipboard

Scrolling based on mouse position?

Open timothyachumba opened this issue 1 year ago • 1 comments

I was wondering if there is a way to trigger scrolling based on the position of the mouse and as you get close to the edges the speed of the scrolling increases.

timothyachumba avatar Apr 21 '23 06:04 timothyachumba

This is how I achieved it.

  window.addEventListener('mousemove', function (event) {
    const clientX = event.clientX
    const percentageMoved = clientX / windowWidth
    const percentageMovedOffset = ((percentageMoved * 1.4) - 0.2)
    const documentWidth = document.querySelector('.scroll-view').scrollWidth
    const maxScrollLeft = documentWidth - windowWidth
    const scrollX = maxScrollLeft * clamp(percentageMovedOffset, 0, 1)
    scroll.scrollTo(scrollX, {
      duration: 1
    })
  })

The only thing i will say is that, it doesn't really have the lerp or inertia effect. Setting the animation duration to 1 was my way around the problem.

timothyachumba avatar Apr 21 '23 11:04 timothyachumba