Skip to content

Instantly share code, notes, and snippets.

@deebloo
Last active September 16, 2021 18:32
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 deebloo/d2c9e023a2179f3e49dc9da578569a60 to your computer and use it in GitHub Desktop.
Save deebloo/d2c9e023a2179f3e49dc9da578569a60 to your computer and use it in GitHub Desktop.
// 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