Last active
March 16, 2018 20:57
-
-
Save s-mage/bca26b996f193f94fdf789d8324c5c7b to your computer and use it in GitHub Desktop.
Prevent scrolling of parent 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
jQuery($ => | |
$(document).on('wheel', '.scrollable', function(ev) { | |
let $el = $(this); | |
let height = $el.outerHeight(true); | |
let delta = ev.originalEvent.deltaY; | |
let up = delta < 0; | |
let prevent = function() { | |
ev.stopPropagation(); | |
ev.preventDefault(); | |
ev.returnValue = false; | |
return false; | |
}; | |
if (!up && (delta > (this.scrollHeight - height - this.scrollTop))) { | |
// Scrolling down, but this will take us past the bottom. | |
$el.scrollTop(this.scrollHeight); | |
return prevent(); | |
} else if (up && (-delta > this.scrollTop)) { | |
// Scrolling up, but this will take us past the top. | |
$el.scrollTop(0); | |
return prevent(); | |
} | |
}) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment