用html+css+js写一个简单的遮罩层 课后练习

首先我们定义好html结构,noe为遮罩层的结构

<body>
    <input type="button" value="按钮">
    <div id="noe">
        <div id="tow">
        </div>
    </div>
</body>

使用css样式,首先使noe遮罩层隐藏

<style>
        #noe {
            width: 100%;
            height: 100%;
            position: fixed;
            top: 0;
            left: 0;
            display: none;
            background-color: rgba(0, 0, 0, 0.6);
        }

        #noe #tow {
            width: 500px;
            height: 350px;
            background-color: #fff;
            margin: 150px auto;
        }
    </style>

下面是js代码

<script>
    // 获取input元素
    var input = document.getElementsByTagName("input")[0];
    // 获取noe元素
    var noe = document.getElementById("noe");
    // 给input添加点击事件
    input.onclick = function () {
        noe.style.display = "block";//显示
    }
    // 给noe添加点击事件
    noe.onclick = function () {
        noe.style.display = "none";//隐藏
    }
</script>

全部代码 下面

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #noe {
            width: 100%;
            height: 100%;
            position: fixed;
            top: 0;
            left: 0;
            display: none;
            background-color: rgba(0, 0, 0, 0.6);
        }

        #noe #tow {
            width: 500px;
            height: 350px;
            background-color: #fff;
            margin: 150px auto;
        }
    </style>
</head>

<body>
    <input type="button" value="按钮">
    <div id="noe">
        <div id="tow">
        </div>
    </div>
</body>

<script>
    // 获取input元素
    var input = document.getElementsByTagName("input")[0];
    // 获取noe元素
    var noe = document.getElementById("noe");
    // 给input添加点击事件
    input.onclick = function () {
        noe.style.display = "block";//显示
    }
    // 给noe添加点击事件
    noe.onclick = function () {
        noe.style.display = "none";//隐藏
    }
</script>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值