Last active
March 29, 2016 09:31
-
-
Save edoardocavazza/f829f32370a0fdb6b333 to your computer and use it in GitHub Desktop.
Prevent wheel scroll propagation to parent scroll panels.
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
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