JavaScript realizes masking and forbidding page scrolling

Time:2020-8-18

In this paper, we share the specific code of JavaScript to achieve masking and page scrolling for your reference. The specific content is as follows

Project requirements: if the page is very long, it is required to add a mask. Click on a specific location and the mask disappears to slide the page. Otherwise, the mask page cannot slide. It should be compatible with both mobile terminal and PC terminal.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name=viewport content="width=device-width,initial-scale=1,user-scalable=no">
  < title > masking and no scrolling
  <style>
    .container{
      width:200px;
      height:2000px;
      background-color:blue;
    }
    .modal{
      width: 100%;
      position:fixed;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      z-index: 999;
      background-color: rgba(0,0,0,0.7);
      background-size: 100%; 
    }
    .p{
      font-size: 50px;
      color:red;
      text-align:center;
    }
  </style>
</head>
<body>
  <div>
    <p>Content</p>
  </div>
  <! -- mask -- >
  <div>
    <p>Button</p>
  </div>
</body>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>

<script>
$(function(){
  //Prevent page sliding after masking
  if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
  //Mobile terminal
    $(".modal").on('touchmove',function(e){
      e.stopPropagation();
      e.preventDefault();
      $('.p').click(function(){
        $(this).parent().hide();    
      })
    })
  } else {
    //PC terminal
    var flag=true;
    $('.p').click(function(){
      $(this).parent().hide();
      $('body').css({  "overflow":'visible'});
      flag=false;   
    })
    if(flag)
      $('body').css({  "overflow":'hidden' });
  }
})
</script>
</html>

The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.