Skip to content

Instantly share code, notes, and snippets.

@monners
Created August 18, 2017 05:40
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 monners/330f8c59b5fbfc9b140b10d948ce5263 to your computer and use it in GitHub Desktop.
Save monners/330f8c59b5fbfc9b140b10d948ce5263 to your computer and use it in GitHub Desktop.
Basic scrollLock class for mobile menus
class ScrollLock {
constructor () {
this.pageBody = document.querySelector('.body');
this.scrollY = 0;
}
saveScrollY = (num) => {
this.scrollY = num;
}
setScrollY = (num) => {
window.scroll(0, num);
}
setScrollOffset = (vOffset) => {
this.pageBody.style.top = `-${vOffset}px`;
}
freezeBodyScroll = () => {
this.saveScrollY(window.scrollY);
this.setScrollOffset(this.scrollY);
this.pageBody.classList.add('is-fixedMobile');
}
unfreezeBodyScroll = () => {
this.pageBody.classList.remove('is-fixedMobile');
// Don't reset scroll position if lock hasn't occurred
if (this.scrollY === 0) return;
this.setScrollOffset(0);
this.setScrollY(this.scrollY);
this.saveScrollY(0);
}
}
export default ScrollLock;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment