Skip to content

Instantly share code, notes, and snippets.

@jayde
Created June 12, 2013 01:24
Show Gist options
  • Save jayde/5762251 to your computer and use it in GitHub Desktop.
Save jayde/5762251 to your computer and use it in GitHub Desktop.
Lock body scroll bar while scrolling div
// url: http://stackoverflow.com/questions/7600454/how-to-prevent-page-scrolling-when-scrolling-a-div-element
// function
$.fn.isolatedScroll = function() {
this.bind('mousewheel DOMMouseScroll', function (e) {
var delta = e.wheelDelta || (e.originalEvent && e.originalEvent.wheelDelta) || -e.detail,
bottomOverflow = this.scrollTop + $(this).outerHeight() - this.scrollHeight >= 0,
topOverflow = this.scrollTop <= 0;
if ((delta < 0 && bottomOverflow) || (delta > 0 && topOverflow)) {
e.preventDefault();
}
});
return this;
};
// call function
$('.scrollable').isolatedScroll();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment