Skip to content

Instantly share code, notes, and snippets.

@jp26jp
Created May 8, 2017 18:09
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 jp26jp/e1caaae2e6edfef2601c982d6f9b4a17 to your computer and use it in GitHub Desktop.
Save jp26jp/e1caaae2e6edfef2601c982d6f9b4a17 to your computer and use it in GitHub Desktop.
Scroll only in hovered div
$('#menu').on('DOMMouseScroll mousewheel', function(ev) {
var $this = $(this),
scrollTop = this.scrollTop,
scrollHeight = this.scrollHeight,
height = $this.height(),
delta = (ev.type == 'DOMMouseScroll' ?
ev.originalEvent.detail * -40 :
ev.originalEvent.wheelDelta),
up = delta > 0;
var prevent = function() {
ev.stopPropagation();
ev.preventDefault();
ev.returnValue = false;
return false;
}
if (!up && -delta > scrollHeight - height - scrollTop) {
// Scrolling down, but this will take us past the bottom.
$this.scrollTop(scrollHeight);
return prevent();
} else if (up && delta > scrollTop) {
// Scrolling up, but this will take us past the top.
$this.scrollTop(0);
return prevent();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment