Last active
July 3, 2020 06:30
-
-
Save theftprevention/5959411 to your computer and use it in GitHub Desktop.
A small jQuery extension that disables the propagation of scroll events from the first element in the set of matched elements. Original function by Troy Alford of Stack Overflow.$("#object").scrollLock() enables the lock, and .scrollRelease() disables it.
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
$.fn.scrollLock=function(){return $(this).on("DOMMouseScroll mousewheel",function(h){var g=$(this),f=this.scrollTop,d=this.scrollHeight,b=g.height(),i=h.originalEvent.wheelDelta,a=i>0,c=function(){h.stopPropagation();h.preventDefault();h.returnValue=false;return false};if(!a&&-i>d-b-f){g.scrollTop(d);return c()}else{if(a&&i>f){g.scrollTop(0);return c()}}})};$.fn.scrollRelease=function(){return $(this).off("DOMMouseScroll mousewheel")}; |
If you don't want to prevent scrolling if element has no scrollbar, use this gist:
jQuery.fn.scrollLock=function(){return $(this).on("DOMMouseScroll mousewheel",function(h){var g=$(this),f=this.scrollTop,d=this.scrollHeight,b=g.height(),i=h.originalEvent.wheelDelta,a=i>0,c=function(){h.stopPropagation();h.preventDefault();h.returnValue=false;return false};if (g.get(0).scrollHeight > g.get(0).clientHeight) {if(!a&&-i>d-b-f){g.scrollTop(d);return c()}else{if(a&&i>f){g.scrollTop(0);return c()}}}})};$.fn.scrollRelease=function(){return $(this).off("DOMMouseScroll mousewheel")};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a ton for making this. FYI you're missing a period in the last statement.$fn instead of $ .fn.