I’m working on a site that overflows horizontally on desktop, as opposed to the usual vertical scroll. This all works fine, however in order to improve the user experience, I have to modify the scrolled amount in order to move larger sections of the screen a time. The problem with the code I’m using is that it doesn’t provide a very smooth experience: The movement seems to be quite clunky.
This is the snippet I’m using and it’s executed on the mousewheel
event:
function scrollHorizontally(e) {
e = window.event || e;
var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
document.documentElement.scrollLeft -= (delta*60); // Multiplied by 60
document.body.scrollLeft -= (delta*60); // Multiplied by 60
e.preventDefault();
}
As you can see, I have to modify the delta * 60 in order to move the scroll bars an adequate amount.
I’d like to know if it’s possible to make this a smooth transition rather than (what seems to be) chunking the user across a fixed pixel amount at a time.
I’ve tried using scrollBy({ left: <amount>, behavior: 'smooth' });
but this doesn’t seem to provide a good user experience: it just seems to ease the movement a little but it feels laggy and inconsistent.
What I’d like is for the user to smoothly animate the desired amount. Does anyone know if this is possible?
I’ve actually built this within a Vue 3 application so would be interested if Vue has any ways to handle this? I’m happy to use native js though.
Thanks
2
Answers
you can try this way.
Let’s try canceling the scroll event and implementing our own physics using animation. I removed the scrollbars to hide the fact that scrolling using the scrollbar wouldn’t work as expected in this simple example.