Skip to content

Instantly share code, notes, and snippets.

@ajliv
Last active August 28, 2016 06:30
Show Gist options
  • Save ajliv/67d425b196a6cccd0b109e315c7f7279 to your computer and use it in GitHub Desktop.
Save ajliv/67d425b196a6cccd0b109e315c7f7279 to your computer and use it in GitHub Desktop.
Prevent window scroll on scrollable element
// 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