I successfully disabled the scroll on the body, thanks to helpful advice from SO! However, I encountered an issue where the body kept jumping back to the top whenever I opened my mobile menu.
If I remove the following code:
overflow: hidden;
The scroll is no longer disabled on the body, but at least it doesn't jump back to the top when I open my mobile menu.
I apply my CSS class .overflowHidden to the body and html elements when the burger menu is open.
const [showBurgerMenu, setShowBurgerMenu] = useState(false) const handleOnClick = () => { const burger = document.querySelector('.burger'); burger.classList.toggle('active'); setShowBurgerMenu(!showBurgerMenu); if (showBurgerMenu === false) { document.querySelector("body").classList.add("overflowHidden"); document.querySelector("html").classList.add("overflowHidden") } else if (showBurgerMenu === true) { document.querySelector("body").classList.remove("overflowHidden"); document.querySelector("html").classList.remove("overflowHidden"); }; }
My CSS class:
.overflowHidden { overflow: hidden; margin: 0; touch-action: none; -ms-touch-action: none; position: fixed; height: 100%; }
Thank you for your assistance! PS: I am using NextJS so I'm not sure if that is relevant.