Skip to content

Instantly share code, notes, and snippets.

@75th
Last active October 17, 2016 16:52
Show Gist options
  • Save 75th/2f85d8e916e3601c920bc1038c049457 to your computer and use it in GitHub Desktop.
Save 75th/2f85d8e916e3601c920bc1038c049457 to your computer and use it in GitHub Desktop.
Convert all scrolling to horizontal scrolling
// Make vertical mousewheel movement scroll a page horizontally.
//
// Seems to work well with Mac trackpads and Magic Mouse on macOS.
// Also works with either scroll direction on macOS.
// If body has class 'scroll-horizontal'
if(document.body.className.split(' ').indexOf('scroll-horizontal') !== -1) {
['wheel', 'mousewheel', 'DOMMouseScroll'].forEach(function(eventName) {
// Test for browser support of event
if(typeof window['on' + eventName] !== 'undefined') {
window.addEventListener(eventName, function(e) {
// If scroll has both horizontal and vertical components, use the greater absolute value
var scrollAmount = Math.abs(e.deltaY) > Math.abs(e.deltaX) ? e.deltaY : e.deltaX;
window.scrollBy(scrollAmount, 0);
e.preventDefault();
});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment