简体   繁体   中英

How to disable background scroll on pop-up?

I'm new to the web development world and wanted to know if there is a way to disable background scrolling.

I've tried z-index for the pop-up to display above all the elements, but some background content was getting overlapped with the pop-up.

I'm not much familiar with JS but was not able to get any help.

Below please find my code

 body { height: 200vh; } .bg-noscroll { } .overlay { position: fixed; top: 0; bottom: 0; left: 0; right: 0; background: rgba(0, 0, 0, 0.7); transition: opacity 500ms; visibility: hidden; opacity: 0; } .overlay:target { visibility: visible; opacity: 1; } .popup { transform: translateY(-60px); margin: 70px auto; padding: 20px; background: #fff; border-radius: 5px; width: 30%; position: relative; transition: all 5s ease-in-out; } .popup .close { position: absolute; top: 20px; right: 30px; transition: all 200ms; font-size: 30px; font-weight: bold; text-decoration: none; color: #333; } .content { height: 250px; } .popup .content { overflow-y: scroll; } @media screen and (max-width: 700px){ .popup{ width: 70%; }
 <body class="bg-noscroll bg-scroll"> <span><a class="popupBG-Disable" href="#popup">Full Recipe</a></span> <div id="popup" class="overlay"> <div class="popup"> <h3>Foxtail Millet Porridge:</h3> <a class="close" href="#">×</a> <div class="content"> <span>Ingredients:<br>here are some things that you'd use to make this<br> isn't this amazing?<br>Yes, it is!<br> this is getting loooooong<br>this will take me a while!<br>oh... yes it will<br>we're getting close<br>and we should be there <br>or not...<br>Im losing hope<br>and patience<br>with how long this is taking<br>I could really cry<br> but we'll get there soon<br>safe and sound<br>free as pie<br>I dont know what I meant by that<br> this is taking long mannnn<br> </span>

Thank you for your help!

Currently, you have to make use of javascript and add or remove the scrollbar-properties or css-class using a hashchange event-listener for example:

window.addEventListener("hashchange", event => {
  const newHash = new URL(event.newURL).hash,
    el = document.getElementById(newHash.substr(1));
  if (el && el.classList && el.classList.contains("overlay")) {
    document.body.style.overflow = "hidden";
    // or document.body.classList.add("bg-noscroll");
  } else {
    document.body.style.overflow = "";
    // or document.body.classList.remove("bg-noscroll");
  }
});

Starting from chromium 101 the support for the :has() -selector has been implemented (experimental flag only) and the current chromium 105 dev channel brings the :has() -selector enabled by default .

With the has() -selector it will be possible using:

body:has(.overlay:target) {
  overflow: hidden;
}

Keep also mind, it may take some more time for other browsers to implement the has() -selector. Therefor the best would be to stick with the javascript method for a while.

I have a live codepen with your original code so you can just copy and paste if you wish.

Using Jquery, we can enable and disable overflow using some simple code:

const modal = document.querySelector("#btn");
const body = document.querySelector("body");
  
const showModal = function (e) {
   modal.classList.toggle("hidden");
  
   if (!modal.classList.contains("hidden")) {
          body.style.overflow = "hidden";
          } else {
           body.style.overflow = "hidden";
          }
   }; // just reversed for re-enabling scroll, as seen in the codepen

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM