Last active
August 2, 2018 22:51
-
-
Save mrtag23/b79a1f44fd82cb9f63c924da5d4facbe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// doesn't work with ios, but works with everything else. | |
var keys = { | |
37: 1, | |
38: 1, | |
39: 1, | |
40: 1 | |
}, | |
preventDefault = function (e) { | |
e = e || window.event; | |
if (e.preventDefault) e.preventDefault(); | |
e.returnValue = false; | |
}, | |
preventDefaultForScrollKeys = function(e) { | |
if (keys[e.keyCode]) { | |
preventDefault(e); | |
return false; | |
} | |
}, | |
// Disables scroll events | |
var 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; | |
}, | |
// Enables scroll events | |
enableScroll = function () { | |
if (window.removeEventListener) window.removeEventListener('DOMMouseScroll', preventDefault, false); | |
window.onmousewheel = document.onmousewheel = null; | |
window.onwheel = null; | |
window.ontouchmove = null; | |
document.onkeydown = null; | |
}; | |
// anaother approach - works with ios | |
function disableScroll(){ | |
document.body.addEventListener('touchmove', preventDefault, { passive: false }); | |
} | |
function enableScroll(){ | |
document.body.removeEventListener('touchmove', preventDefault, { passive: false }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment