美文网首页
遮罩层弹出后,禁止被遮罩部分滚动

遮罩层弹出后,禁止被遮罩部分滚动

作者: 锦锦_jane | 来源:发表于2020-09-09 15:18 被阅读0次
  • 需求:在弹出蒙层之后,底部页面不会滚动,关闭蒙层恢复滚动时,还在原先的位置

  • 网上查找的方法:

    • 方案:在禁止时,document不滚动,body固定定位+宽度100%,恢复时,document滚动,body静态定位
    • 缺点:允许滚动时,页面会自动回到顶部
  • 解决方案: 在禁止滚动时,记录当前位置,固定定位时也定在当前位置,恢复时,定在当前位置

  • jQuery写法:

var topPos; //记录弹出蒙层前,页面的位置
//禁止滚动条滚动
function stopScroll() {
    topPos = $(document).scrollTop(); //记录页面的位置
    console.log(topPos);
    $('document').css('overflow','hidden'); //隐藏滚动条
    $('body').css({
          'position': 'fixed', //固定定位
          'top': -topPos + 'px', //让底层页面定位固定在蒙层弹出之前的位置
          'width': '100%' 
    });
}
//允许滚动条滚动
function openScroll() {
    $('document').css('overflow','scroll'); //显示滚动条
    $('body').css('position', 'static'); //静态定位
    $('html,body').scrollTop(topPos); //关闭蒙层,让页面蒙层弹出之前的位置
    console.log($('html,body').scrollTop());
}
  • demo
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>弹出遮罩层后,如何禁止底层页面的滚动</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .container {
            max-width: 750px;
            height: 2000px;
            margin: 0 auto;
            background-color: teal;
            font-size: 32px;
            font-weight: bold;
            color: #000;
            text-align: center;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }

        .showBtn {
            position: fixed;
            top: 50%;
            left: 0;
            transform: translateY(-50%);
            width: 100px;
            height: 50px;
            font-size: 20px;
            font-weight: bold;
            line-height: 50px;
            color: #fff;
            background-color: cornflowerblue;
            text-align: center;
            border-radius: 25px;
            margin: 30px auto 0;
            cursor: pointer;
        }

        .hideBtn {
            width: 100px;
            height: 50px;
            font-size: 20px;
            font-weight: bold;
            line-height: 50px;
            color: #fff;
            background-color: cornflowerblue;
            text-align: center;
            border-radius: 25px;
            margin: 30px auto 0;
            cursor: pointer;
        }

        .mask {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, .6);
            z-index: 999;
            font-size: 20px;
            color: #fff;
            font-weight: bold;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            display: none;
        }
        .bottomp{
            font-size: 32px;
            font-weight: bold;
            color: #000;
            text-align: center;
        }
    </style>
    <script src="./jquery.js"></script>
</head>

<body>
    <div class="container">
        <p>底部页面</p>
        <p>1</p>
        <p>2</p>
        <p>3</p>
        <p>4</p>
        <p>5</p>
        <p>6</p>
        <p>7</p>
        <p>8</p>
        <p>9</p>
        <p>10</p>
        <p>11</p>
        <p>12</p>
        <p>13</p>
        <p>14</p>
        <p>15</p>
        <p>16</p>
        <p>17</p>
        <p>18</p>
        <p>19</p>
        <p>20</p>
        <p>21</p>
        <p class="bottomp">底部</p>
        <div class="showBtn">显示蒙层</div>
    </div>
    <div class="mask">
        <p>蒙层</p>
        <div class="hideBtn">关闭蒙层</div>
    </div>
</body>
<script>
    $('.showBtn').click(function () {
        $('.mask').css('display', 'flex');
        stopRoll()
    })
    $('.hideBtn').click(function () {
        $('.mask').hide()
        openRoll()
    })

    var topPos;

    function openRoll() {
        $('document').css('overflow','scroll')
        $('body').css('position', 'static');
        $('html,body').scrollTop(topPos)
        console.log($('html,body').scrollTop());
        
    }

    function stopRoll() {
        topPos = $(document).scrollTop()
        console.log(topPos);
        $('document').css('overflow','hidden')
        $('body').css({
            'position': 'fixed',
            'top': -topPos + 'px',
            'width': '100%'
        });
    }
</script>
</html>

相关文章

网友评论

      本文标题:遮罩层弹出后,禁止被遮罩部分滚动

      本文链接:https://www.haomeiwen.com/subject/rgndektx.html