效果

oq3yt-eucb5.gif

实现方式

将弹出框初始的display设置为none,通过js改变display

代码

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .back {
            display: flex;
            justify-content: center;
            align-items: center;
            /* width: 200px; */
            height: 100vh;
            background-color: #ccc;
        }

        .front {
            display: none;
            position: fixed;
            /* 使用 fixed 定位 */
            top: 50%;
            /* 元素顶部距离视口顶部的距离为视口高度的一半 */
            left: 50%;
            /* 元素左侧距离视口左侧的距离为视口宽度的一半 */
            transform: translate(-50%, -50%);
            /* 水平和垂直居中 */
            width: 200px;
            height: 200px;
            background-color: rgb(237, 171, 171);
            z-index: 999;
        }
    </style>
</head>

<body>
    <div class="back">
        <button>点击弹出框</button>
    </div>
    <div class="front" id="front">
        <!-- 这是上层 -->
        <!-- 表单 -->
        <form action="">
            <input type="text">
            <span id="delete-form">x</span>
            <input type="submit" value="提交">
        </form>
    </div>

    <script>
        document.querySelector('button').addEventListener('click', () => {
            let front = document.getElementById('front');
            front.style.display = 'block';
        })

        document.getElementById('delete-form').addEventListener('click', () => {
            let front = document.getElementById('front');
            front.style.display = 'none';
        })
    </script>
</body>

</html>