Skip to content

Instantly share code, notes, and snippets.

@mattsacks
Created May 22, 2013 22:46
Show Gist options
  • Save mattsacks/5631556 to your computer and use it in GitHub Desktop.
Save mattsacks/5631556 to your computer and use it in GitHub Desktop.
Prevent back/forward gestures when scrolling an element left/right on OS X
// prevent back/forward gestures when scrolling an element left/right on OS X
var preventHistorySwipe = function(element) {
// not even on the right platform bro
if (navigator.userAgent.indexOf('Macintosh') === -1) return;
element.addEventListener('mousewheel', function(e) {
var x = e.wheelDeltaX;
// scrolling up or down? then i don't give a shit
if (x === 0) return;
// get the current scroll
var scroll = element.scrollLeft;
var maxScroll = element.scrollWidth - element.clientWidth;
// scrolled to the left and scrolling left
if (scroll === 0 && x > 0)
e.preventDefault();
// scrolled to the right and scrolling right
else if (scroll === maxScroll && x < 0)
e.preventDefault();
});
return element;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment