How can one prevent scrolling when the off-canvas menu is open? I want the focus to be on the content of the nav menu when it’s open, so I want to limit/lock the visitor’s ability to scroll and distract themselves.
The off-canvas nav menu (hamburger button) right here is a perfect example.
Try to use this script (been used for preventing scroll with custom modals), .menu-button-class and .menu-class-name should bechanged on classnames that you have:
Hi @sabanna, I tried using the script you mentioned but it’s not working for me as I only have one button to perform the whole thing (actually, it’s a link block that opens and closes a side menu). Do you know if it’s possible to do something like on first click prevent scrolling, on second click allow scrolling?!
I tried giving the same class to .menu-button-class and .menu-class-name and nothing happens. I also tried using your script having two buttons (I already deleted the close button), with different class names and I couldn’t get this to work (which is odd because I have managed to make this work in the past with modals…)
Do you mind taking a look, please? https://preview.webflow.com/preview/pnc-test?preview=df514a3d9bfeb25d60bfd3e01e49c264
Hi @sabanna. I used your code and replaced the .menu-button-class and .menu-class-name and when I opened the nav, scrolling was prevented. So it worked!
The problem is, after I close the navigation, I can’t scroll at all on my site. The entire page is fixed. Any ideas to make the page scrollable again?
My apology for the delay with the answer.
Could you provide the link to published site? Because I am not able to see the custom code that you are using, so it is hard to find the reason of the problem.
Nope. I tried moving the code from the footer to the header, but that made it not work. So I moved it back to the footer. I’m still having the issue where, after I close the nav menu, the site is stalled. I am using this MagicMenu code in my custom code as well…maybe that’s having some effect?
Just to clarify, for the menu-class-name, that refers to the Nav Menu and not the Navbar or Nav Container, right? I’m using the Nav Menu class name.
I do have a separate close button so maybe that will not work. But this would be a nice feature to implement because the scrolling under the menu is distracting.
No, it works like this: When menu button (or any other object which class you will add) is clicked, js code change the CSS of the body and make it overflow: hidden >>> that makes the body unscrollable.
Then next part of the code takes click from another object (close button, menu, modal background, etc.) and change the CSS of the body again, making it overflow: auto >>> that makes the body scrolling again.
If you have the same classes for both “click events” on each page (like a menu, for example) then it makes sense to add this in site settings custom code.
If this code has to be used with a modal popup on some page, then add to page custom code.
I just realize that link in your original (first) post in this topic is not your site. Could you share the read-only link and link to published site, please?