Scroll right on scroll down left on scroll up Javascript

I’ve probably tested 30 different snippets of code. Yeah, yeah, I should probably understand what I’m actually doing first. But here’s the one that most reliably worked. Obviously, from css-tricks who writes it in jQuery. Below is the vanilla version with reliable flair.

function scrollHandler (event) {
    event.preventDefault()
    event.stopPropagation()
    window.scrollBy({ left: event.deltaY })
}
window.addEventListener('scroll', event => scrollHandler(event), {passive: false})

Leave a Comment