Skip to content

Instantly share code, notes, and snippets.

@jrc03c
Created September 8, 2023 16:25
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 jrc03c/74e8fdedd3cff81283e9cef6080f1c91 to your computer and use it in GitHub Desktop.
Save jrc03c/74e8fdedd3cff81283e9cef6080f1c91 to your computer and use it in GitHub Desktop.
Temporarily disable page scrolling when modal or menu is open
let scrollY = 0
// to disable scrolling:
scrollY = window.scrollY
document.body.style.position = "fixed"
document.body.style.top = `-${scrollY}px`
// to enable scrolling again:
document.body.style.position = ""
document.body.style.top = ""
window.scrollTo(0, parseInt(scrollY || 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment