Skip to content

Instantly share code, notes, and snippets.

@s-mage
Last active March 16, 2018 20:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save s-mage/bca26b996f193f94fdf789d8324c5c7b to your computer and use it in GitHub Desktop.
Save s-mage/bca26b996f193f94fdf789d8324c5c7b to your computer and use it in GitHub Desktop.
Prevent scrolling of parent element.
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