css解决移动端弹框下背景滑动问题

如何处理移动端弹框下页面滑动的问题

我们不需要依靠js来处理,只需要设置简单的css就可以啦

重点:最外层设置absolute定位

代码来了:

<!DOCTYPE html>
<html>
  <head>
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, shrink-to-fit=no"
    />
    <style>
      .container {
        width: 100%;
        position: absolute; /* 给最外层元素设置绝对定位,让其内部进行滚动 */
        top: 0;
        bottom: 0;
        right: 0;
        left: 0;
        overflow-y: scroll;
        -webkit-overflow-scrolling: touch; /* 增加弹性 */
        user-select: none; /* 移动端一般会设置不可选中 */
      }
      .con {
        width: 100%;
        height: 1000px;
        text-align: center;
        padding-top: 500px;
        background: #f94f4f;
      }
      #dialog {
        position: fixed;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        background: rgba(0, 0, 0, 0.5);
        display: none;
        justify-content: center;
        align-items: center;
      }
      .wrapper {
        width: 280px;
        height: 200px;
        border-radius: 5px;
        background: #fff;
        font-size: 16px;
        display: flex;
        justify-content: center;
        align-items: center;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="con">
        <button onClick="openDialog()">open</button>这是一个1000px的图片
      </div>
    </div>
    <div id="dialog">
      <div class="wrapper">
        <button onClick="closeDialog()">close</button>这是一个弹框
      </div>
    </div>

    <script>
      const openDialog = () => {
        document.getElementById("dialog").style.display = "flex";
      };
      const closeDialog = () => {
        document.getElementById("dialog").style.display = "none";
      };
    </script>
  </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值