A problem that front-end developers often encounter is creating a pop-up window to prompt users with information. When this pop-up window pops up, it is often accompanied by a gray mask layer that blocks the page content. At the same time, the entire page is covered by this mask and cannot be clicked or scrolled. Solution 1: Control overflow and prohibit scrolling (not compatible with iOS) It is very simple to create this effect on the PC. You only need to set the height of the HTML to 100% to fill the screen, and set the overflow of the HTML to hidden to ensure that the page cannot be scrolled. html.style.overflow="hidden"; html.style.height="100%"; body.style.overflow="hidden"; body.style.height="100%"; The reason is that the mobile terminal is based on touch events. To prohibit scrolling based on touch events, we must, on the basis of prohibiting scrolling in HTML, add a wrapping block-level element to the content that needs to be prohibited from scrolling, and then set the height of this wrapping block-level element to 100% and set overflow:hidden;. Then here we think that body wraps the entire page, which is the block-level element we need. Setting it to prohibit scrolling can ensure that the sliding time of the mobile page will not trigger page scrolling. html.style.overflow="visible"; html.style.height="auto"; body.style.overflow="visible"; body.style.height="auto"; These styles are exactly the default styles of the corresponding CSS properties. Solution 2: Absolute/fixed layout prevents gesture scrolling events from bubbling (not effective on PC) It is precisely because the scrolling of the mobile terminal is based on the touch event of the screen that the second solution was born (Taobao Mobile uses this solution). The complete test source code of solution 2 is posted below: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> .main-content{ position:relative; width:100%; background-color:#ccc; height:2000px; } .main-content .trigger{ width:200px; height:100px; font-size:30px; color:#000; } .main-content .bottom{ position:absolute; bottom:0; left:0; width:100%; height:200px; background-color:red; } .black-shield{ position:fixed; top:0; left:0; width:100%; height:100%; background-color:rgba(10,10,10,0.4); z-index:10; } .black-shield .info{ font-size:40px; color:#000; border:1px solid; z-index:20; } </style> </head> <body> <div class="main-content"> <button id="trigger" class="trigger">On/Off</button> <div class="bottom"></div> </div> <div id="shield" class="black-shield" style="display:none;"> <div id="info" class="info">After the current black screen pops up, the page should not be slidable. Click the current text to close the black screen</div> </div> <script> function test2(){ var showShield=false; var shield = document.getElementById("shield"); var trigger = document.getElementById("trigger"); var info = document.getElementById("info"); var body = document.querySelector("body"); var html = document.querySelector("html"); //Click to display the black screen trigger.addEventListener("click",function(){ shield.style.display="block"; },false); //Click to close the black curtain info.addEventListener("touchstart",function(){ shield.style.display="none"; },false); //Block touch events in the black screen layer shield.addEventListener("touchstart",function(e){ e.stopPropagation(); e.preventDefault(); },false); } test2(); </script> </body> </html> This is the end of this article about the front-end page pop-up mask to prevent page scrolling. For more relevant content about the pop-up mask to prevent page scrolling, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope that everyone will support 123WORDPRESS.COM in the future! |
<<: CSS to achieve Cyberpunk 2077 style visual effects in a few steps
>>: Sample code using vue-router in html
I encountered this problem when I was making the ...
Table of contents 1. We must ensure that the vue/...
Let's first look at several ways to submit a ...
Table of contents Preface 1. With vue-cli 1. Defi...
Table of contents 1.1Tinyint Type Description 1.2...
Table of contents Preface Ajax serial and paralle...
1. Download First of all, I would like to recomme...
This article example shares the specific code of ...
Enable remote access Enable remote access rights ...
Preface Linux does not have a prominent Recycle B...
Here is a Vue single sign-on demo for your refere...
Copy code The code is as follows: <style type=...
Table of contents 1. Understanding the Equality R...
Introduction MySQL should be a very common databa...
Table of contents Preface text 1. Closure 1.1 Wha...