Skip to content

Instantly share code, notes, and snippets.

@edoardocavazza
Last active March 29, 2016 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edoardocavazza/f829f32370a0fdb6b333 to your computer and use it in GitHub Desktop.
Save edoardocavazza/f829f32370a0fdb6b333 to your computer and use it in GitHub Desktop.
Prevent wheel scroll propagation to parent scroll panels.
function TrapScroller(scroller) {
function callback(ev) {
ev.stopPropagation();
var d = ev.deltaY || 0;
if (
(scroller.scrollHeight !== scroller.offsetHeight) && (
(d < 0 && scroller.scrollTop === 0) ||
(d > 0 && (scroller.scrollTop >= scroller.scrollHeight - scroller.offsetHeight)) ||
(scroller.scrollHeight === scroller.clientHeight))
) {
ev.preventDefault();
}
}
scroller.addEventListener('mousewheel', callback);
scroller.addEventListener('scroll', callback);
scroller.addEventListener('DOMMouseScroll', callback);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment