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
// Freezes document scrolling without remove scrollbar. | |
// This gets rid of the annoying jump that happens when you set the body overflow to hidden | |
// Maintains scroll position on the page | |
export function freezeScroll() { | |
document.body.style.top = -document.documentElement.scrollTop + 'px'; | |
document.body.style.position = 'fixed'; | |
document.body.style.left = '0'; | |
document.body.style.right = '0'; | |
document.body.style.overflowY = 'scroll'; | |
} | |
// Releases the scroll freeze previously applied | |
// scrolls the window back to it's original position | |
export function releaseScroll() { | |
const top = Math.abs(parseFloat(document.body.style.top)); | |
document.body.style.top = ''; | |
document.body.style.position = ''; | |
document.body.style.left = ''; | |
document.body.style.right = ''; | |
document.body.style.overflowY = ''; | |
window.scrollTo(0, top); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment