Last active
August 28, 2016 06:30
-
-
Save ajliv/67d425b196a6cccd0b109e315c7f7279 to your computer and use it in GitHub Desktop.
Prevent window scroll on scrollable element
This file contains 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
// Usage: | |
// var el = docuemnt.getElementById('scrollable-div'); | |
// el.addEventListener('wheel', preventWindowScroll, false); | |
function preventWindowScroll(e) { | |
var direction = e.deltaY < 0 ? 'up' : 'down'; | |
var el = e.currentTarget; | |
var atTop = el.scrollTop <= 0; | |
var atBottom = (el.scrollHeight - (el.clientHeight + el.scrollTop) <= 0); | |
if ((direction === 'up' && atTop) || (direction === 'down' && atBottom)) { | |
e.preventDefault(); | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment