遮罩层效果实现

1 篇文章 0 订阅

先看下效果:
在这里插入图片描述

代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    body,
    div,
    button{
      margin: 0;
      padding: 0;
    }
    .container{
      width: 100vw;
      height: 100vh;
    }
    .top{
      position: fixed;
      top: 0;
      width: 100%;
      z-index: 100;
    }
    .top button{
      width: 100%;
      height: 40px;
      border: none;
      background-color: aliceblue;
      outline: none;
    }
    .modal{
      position: absolute;
      width: 100%;
      height: calc(100vh - 40px);
      background-color: rgba(100, 100, 100, .5);
      z-index: 100;
      display: none;
    }
    .bottom{
      margin-top: 40px;
    }
    .box{
      height: 40px;
      margin-top: 20px;
      background-color: lightgoldenrodyellow;
    }
  </style>
</head>
<body>
  <script>
    window.onload = function(){
      function defferScroll(e){
        e.preventDefault()
      }
      const btn = document.getElementsByTagName('button')[0]
      btn.addEventListener('click', function(e){
        e.stopPropagation()
        const modal = document.getElementsByClassName('modal')[0]
        // console.log(modal)
        modal.setAttribute('style', 'display: block;')
        document.body.addEventListener("touchmove", defferScroll, {passive: false});
        document.body.addEventListener("wheel", defferScroll, {passive: false});
      })

      document.body.addEventListener('click', function(e){
        const modal = document.getElementsByClassName('modal')[0]
        // console.log(modal)
        modal.removeAttribute('style')
        document.body.removeEventListener("touchmove", defferScroll, {passive: false});
        document.body.removeEventListener("wheel", defferScroll, {passive: false});
      })
    }
  </script>
  <div class="container">
    <div class="top">
      <button>点击弹出遮罩层</button>
      <div class="modal"></div>
    </div>
    <div class="bottom">
      <div class="box">1</div>
        <div class="box">2</div>
        <div class="box">3</div>
        <div class="box">4</div>
        <div class="box">5</div>
        <div class="box">6</div>
        <div class="box">7</div>
        <div class="box">8</div>
        <div class="box">9</div>
        <div class="box">10</div>
        <div class="box">11</div>
        <div class="box">12</div>
        <div class="box">13</div>
        <div class="box">14</div>
        <div class="box">15</div>
        <div class="box">16</div>
        <div class="box">17</div>
        <div class="box">18</div>
    </div>
  </div>
</body>
</html>

不过这个代码有个bug,换成自适应移动端的时候,最上面固定的那个蓝色的按钮会动,我暂时还改不了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值