Thanks for this script it saves me a lot of time.
However, recently, I noticed that it doesn’t work anymore (when my modal is active, the body is still scrolling. But last month this piece of code was just working right…).
May be Webflow changed his settings about this…
I’ve just added “,html” just near “body” in the code and it worked for me.
I thought it could be helpful for anyone else…
I know it’s long time passed, but since many webflowers are using this workaround just wanted to share another code snippet that will work with Webflow navbar component.
This code will prevent the page from scrolling when a menu is open:
UPDATE:
There is a bug when iOS doesn’t prevent users from scrolling past the modal if there is content that exceeds the viewport height, despite adding overflow:hidden to the CSS.
iOS bug created in 2016 solved only in May 2019 but they have no idea when it will be released. https://bugs.webkit.org/show_bug.cgi?id=153852
So I had to update the code below with the additional enhancement which detects the OS of the device and does a bit of a different approach if it is iOS.
<script>
// Detecting if it is an iOS device, true/false
var iOS = !!navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform);
$(document).ready(function(){
// Defining that "overlay" is the element that has a changing display value
var overlay = document.querySelector('.w-nav-overlay');
// Creating our mutation observer, which we attach to overlay later
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutationRecord) {
// Checking if it's the style attribute got changed and if display value now set to 'none'?
if( mutationRecord.attributeName === 'style' && window.getComputedStyle(overlay).getPropertyValue('display') !== 'none'){
//Overlay's display value is no longer 'none', now changing the "body" styles:
if (iOS) {
// for iOS devices:
var x = $(window).scrollTop().toFixed()
$('body').css({'overflow': 'hidden',
'position': 'fixed',
'top' : '-' + x + 'px',
'width': '100vw'});
}
// for all other devices:
$('body').css('overflow', 'hidden');
}
//Overlay's display value back to 'none' , now changing the "body" styles again:
else {
if (iOS) {
// for iOS devices:
var t = $('body').css('top').replace('-','').replace('px','')
$('body').css({'overflow': 'auto',
'position': '',
'width': '100vw'});
$('body').animate({scrollTop:t}, 0);
}
// for all other devices:
$('body').css('overflow', '');
}
});
});
// Attach the mutation observer to overlay, and only when attribute values change
observer.observe(overlay, { attributes : true, attributeFilter : ['style']});
});
</script>
The snippet can be used regardless of the class-names you are using in your project.
There is a bug when iOS doesn’t prevent users from scrolling past the modal if there is content that exceeds the viewport height, despite adding overflow:hidden to the CSS.
So I had to update the code ( in the previous comment) with the additional enhancement which detects the OS of the device and does a bit of a different approach if it is iOS.
Hi, it should work in published site, can you check and clarify?
Edit: also, published site, previewed in mobile browser, not desktop browser.
Edit 2: I just checked on my own site, solution seemed to not work as it previously did. My suspicion is that there was some fix on iOS 13. I changed overflowY: ‘scroll’, to overflow: ‘scroll’, and
overflowY: ‘hidden’ to overflow: ‘hidden’, seemed to fix the problem and eliminated scroll together with page jumping. Let me know if that works for you @Philemon
@sabanna@jamesgreeny did you find a solution for this? Having the same issue. I’d like to use the same trigger. So on the first click the element disables scroll and on the second click it enables scroll?
@joseph-webflow was just searching through the forums looking for a solution and happened to come across this, the most recent answer, and it works like a charm on both desktop and mobile. Thank you!
Works for me but having an issue where none of the links within the menu work. The page doesn’t refresh to the link tapped. Is something blocking the links from being active?
Can someone please help me out with a custom code on how to disable scrolling of the page – my menu open and menu close interactions are both on the same element.
Sorry for very late reply, I’m finally back at my portfolio, and yes I just made the same changes for overflowY >to> overflow and now it works fine! Thanks!
I tried all the snippets of code in this thread but none of it seemed to work… anyone figured out a different way of going about this? Or could someone help me out by taking a look: