Created
August 18, 2017 05:40
-
-
Save monners/330f8c59b5fbfc9b140b10d948ce5263 to your computer and use it in GitHub Desktop.
Basic scrollLock class for mobile menus
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
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