-
-
Save jp26jp/e1caaae2e6edfef2601c982d6f9b4a17 to your computer and use it in GitHub Desktop.
Scroll only in hovered div
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
$('#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