I am looking for a way to prevent the body from scrolling when the overlay 'popup-section' is opened and to only allow scrolling on the 'popup-section' div. I am looking for a solution using javascript, however, I am not very experienced in JS
Does anyone have any suggestions?
$('#toggle-menu').click(function() {
$(this).toggleClass('active');
$('.popup-section').toggleClass('open');
$('html').toggleClass('open');
});
$('.popup-section').click(function() {
$(this).toggleClass('active');
$('.popup-section').removeClass('open');
$('.button_container').removeClass('active');
$('html').removeClass('open');
});
.popup-section{
display: none;
opacity: 0;
height: 100vh;
left: 0;
right: 0;
width: 100vw;
overflow: scroll;
}
.popup-section.open {
display: block;
opacity: 1;
z-index: 99;
}
.popup-background {
height: 100vh;
width: 100vw;
background-color: #ccbcaf;
z-index: 90;
cursor: pointer;
position: fixed;
overflow: scroll;
top: 0;
}
<div class="button_container open" id="toggle-menu">
<span class="top"></span>
<span class="bottom"></span>
</div>
<div class="popup-section">
<div class="popup-background" title="">
<!--CONTENT-->
</div>
</div>
To hide the scrollbar and disable scrolling, we can use the CSS overflow property. This property determines what to do with content that extends beyond the boundaries of its container. To prevent scrolling with this property, just apply the rule overflow: hidden to the body (for the entire page) or a container element.
If we know the top of the scroll location and add it to our CSS, then the body will not scroll back to the top of the screen, so problem solved. We can use JavaScript for this by calculating the scroll top, and add that value to the body styles: // When the modal is shown, we want a fixed body document.
Disabling scroll with only CSS There's another way to disable scrolling that is commonly used when opening modals or scrollable floating elements. And it is simply by adding the CSS property overflow: hidden; on the element you want to prevent the scroll.
ontouchmove = (e) => { e. preventDefault(); return false; }; locks the body scroll, but ALSO locks the scroll of a target element (eg. modal).
Your JQuery actually seems to be mostly working. The popup-section opens and scrolls. To stop the body underneath from scrolling, you can add this line to your existing JQuery:
$('html, body').css({ position: 'fixed'});
Or, you might prefer the effect of this instead: $('html, body').css({ overflow: 'hidden'});
If you want to undo the effect on clicking the popup section, you can reverse it in your next function:
<script>
$('#toggle-menu').click(function() {
$(this).toggleClass('active');
$('.popup-section').toggleClass('open');
$('html').toggleClass('open');
$('html, body').css({ position: 'fixed'}); //stops body scroll
});
$('.popup-section').click(function() {
$(this).toggleClass('active');
$('.popup-section').removeClass('open');
$('.button_container').removeClass('active');
$('html').removeClass('open');
$('html, body').css({ position: 'relative'}); //restarts body scroll
});
</script>
The snippet was throwing an error when I used JQuery, but it works great on my server with no errors. I had to include the example in an iframe
just to show how the popup scrolls while the body stops scrolling.
<iframe src = "https://www.scriptbarrel.com/examples/popup.htm">
</iframe>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With