Skip to content

Instantly share code, notes, and snippets.

@mrfolkblues
Created January 16, 2018 20:38
Show Gist options
  • Save mrfolkblues/4a62f900c5367bf2484357b866f4465c to your computer and use it in GitHub Desktop.
Save mrfolkblues/4a62f900c5367bf2484357b866f4465c to your computer and use it in GitHub Desktop.
Prevent different types of scrolling, with disable and enable functions
/* Prevent Different Types of scrolling */
// left: 37, up: 38, right: 39, down: 40,
// spacebar: 32, pageup: 33, pagedown: 34, end: 35, home: 36
var keys = { 37: 1, 38: 1, 39: 1, 40: 1 };
function preventDefault(e) {
e = e || window.event;
console.log(e.target);
if (e.preventDefault) e.preventDefault();
e.returnValue = false;
}
function preventDefaultForScrollKeys(e) {
if (keys[e.keyCode]) {
preventDefault(e);
return false;
}
}
window.disableScroll = function () {
if (window.addEventListener) // older FF
window.addEventListener('DOMMouseScroll', preventDefault, false);
window.onwheel = preventDefault; // modern standard
window.onmousewheel = document.onmousewheel = preventDefault; // older browsers, IE
window.ontouchmove = preventDefault; // mobile
document.onkeydown = preventDefaultForScrollKeys;
};
window.enableScroll = function () {
if (window.removeEventListener) window.removeEventListener('DOMMouseScroll', preventDefault, false);
window.onmousewheel = document.onmousewheel = null;
window.onwheel = null;
window.ontouchmove = null;
document.onkeydown = null;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment