Skip to content

Instantly share code, notes, and snippets.

@jonathan-beebe
Forked from theftprevention/jquery-scrolllock.js
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jonathan-beebe/9252009 to your computer and use it in GitHub Desktop.
Save jonathan-beebe/9252009 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. In response to this SO question: http://stackoverflow.com/a/16324762/123781
$.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")
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment