运用HTML+CSS+JS实现一个遮罩层

参考代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>测试</title>
    <style type="text/css">
        .main{
            height: 1200px;
            background-color: orange;
        }
        .box-1{
            background-color: black;
            opacity: 0.6;
            position: fixed;
            z-index: 100000;
            display: none;
            top: 0;
            left: 0;
        }
        #login{
            cursor: pointer;
        }
        .box-1 >div{
            float: left;
        }
        .box-main{
            width: 95%;
            height: 100%;
        }
        .tuichu{
            width: 5%;
        }
        .tuichu p{
            background-color: black;
            color: white;
            font-weight: bold;
            font-size: 20px;
            width: 100%;
            text-align: center;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="main">
        <div id="login">登录</div>
        <a href="https://wwww.baidu.com">baidu</a>
    </div>
    <div class="box-1">
        <div class="box-main"></div>
        <div class="tuichu">
            <p>X</p>
        </div>
    </div>
    <script type="text/javascript">
        var login = document.querySelector('#login');
        var box_1 = document.querySelector('.box-1');
        var tuichu = document.querySelector('.tuichu');
        login.onclick = function(){
            let w = window.innerWidth;
            let h = window.innerHeight;
            console.log(w,h);
            box_1.style.width = w +'px';
            box_1.style.height = h + 'px';
            box_1.style.display = 'block';
        }

        tuichu.onclick = function(){
            box_1.style.display = 'none';
        }
    </script>
</body>
</html>

效果如下:

posted @ 2022-07-05 07:24  坚持不懈的大白  阅读(171)  评论(0编辑  收藏  举报
@format