Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent body scrolling once overlay is open?

Tags:

javascript

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>
like image 954
glittergirl Avatar asked Jun 01 '19 02:06

glittergirl


People also ask

How do I stop my screen from scrolling CSS?

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.

How do you stop background scrolling when Bootstrap 3 modal open?

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.

How do I turn off background scrolling?

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.

How do I lock my body scroll?

ontouchmove = (e) => { e. preventDefault(); return false; }; locks the body scroll, but ALSO locks the scroll of a target element (eg. modal).


1 Answers

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>
like image 89
Bman70 Avatar answered Nov 10 '22 18:11

Bman70