javascript实现蒙版与禁止页面滚动

本文介绍了一种实现页面长内容上加蒙版的方法,并通过点击特定位置关闭蒙版来重新启用页面滚动,适用于移动端及PC端。

蒙版与禁止内容滚动

项目需求:页面很长,要求加个蒙版,点击特定位置蒙版消失可以滑动页面,否则蒙版存在页面不可以滑动;要同时兼容移动端和pc端。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name=viewport content="width=device-width,initial-scale=1,user-scalable=no">
    <title>蒙版与禁止滚动</title>
    <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 class="container">
        <p>内容</p>
    </div>
    <!-- 蒙版 -->
    <div class="modal">
        <p class="p">按钮</p>
    </div>
</body>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>

<script>
$(function(){
    // 阻止蒙版后页面滑动
    if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
    //移动端
        $(".modal").on('touchmove',function(e){
            e.stopPropagation();
            e.preventDefault();
            $('.p').click(function(){
                $(this).parent().hide();       
            })
        })
    } else {
        //PC端
        var flag=true;
        $('.p').click(function(){
            $(this).parent().hide();
            $('body').css({    "overflow":'visible'});
            flag=false;     
        })
        if(flag)
            $('body').css({    "overflow":'hidden' });
    }
})
</script>
</html>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值