locomotive-scroll
locomotive-scroll copied to clipboard
Scrolling based on mouse position?
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.
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.