为网页添加蒙板效果和弹出层js

   有的时候我们需要实现弹出一个层然后下面的层被一层蒙板蒙起来使用户不能点击里面的按钮效果,这一效果具体实现的代码如下:这是一个测试效果用的html,可以直接拷贝到html文件中执行。

<html>
<head>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script type="text/javascript">
//获得坐标
        function getPosition() {
            var top = document.documentElement.scrollTop;
            var left = document.documentElement.scrollLeft;
            var height = document.documentElement.clientHeight;
            var width = document.documentElement.clientWidth;
            //top=500;
            left = 200;
            //height=0;
            width = 500;
            height=300;
            top=200;
            return { top: top, left: left, height: height, width: width };
        }
        //屏蔽输入,显示蒙板
        function showMask(id) {
            var obj = document.getElementById(id);
            obj.style.width = document.body.clientWidth;
            obj.style.height = document.body.clientHeight;
            obj.style.display = "block";
        }
        //隐藏蒙板
        function hideMask(id) {
            document.getElementById(id).style.display = "none";
        }
        //显示登录
        function showPop(id) {
            showMask('mask');
            var width = 300;  //弹出框的宽度
            var height = 170;  //弹出框的高度
            var obj = document.getElementById(id);
            obj.style.display = "block";
            obj.style.position = "absolute";
            obj.style.zindex = "10";
            obj.style.overflow = "hidden";
            obj.style.width = width + "px";
            obj.style.height = height + "px";
            var Position = getPosition();
            leftadd = (Position.width - width) / 2;
            topadd = (Position.height - height) / 2;
            obj.style.top = (Position.top + topadd) + "px";
            obj.style.left = (Position.left + leftadd) + "px";
            window.onscroll = function() {
                var Position = getPosition();
                obj.style.top = (Position.top + topadd) + "px";
                obj.style.left = (Position.left + leftadd) + "px";
            };
            
        }
        //隐含
        function hidePop(id) {
        hideMask('mask');
            document.getElementById(id).style.display = "none";
        }
</script>
</head>

<body>
<div>
<input type="button" value="测试弹出层" οnclick="showPop('tcc')"/>
</div>

<!--蒙板-->
        <div id="mask" style="filter: Alpha(opacity=30); -moz-opacity: 0.3; -khtml-opacity: 0.3;
            opacity: 0.3; background-color: #000; width: 100%; height: 100%; z-index: 5px;
            position: absolute; left: 0; top: 0; display: none; overflow: hidden;">
        </div>
        <!--蒙板结束-->
        <!--弹出层---->
        <div id="tcc" style="display:none;background-color:#00ffff;">
        <input type="button" id="test" value="隐藏弹出层" οnclick="hidePop('tcc');"/>
        </div>
        
        
</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值