原生JS實(shí)現(xiàn)彈出、關(guān)閉模態(tài)框

效果頁(yè)面
jsbin代碼

<!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>
        .clearfix:after {
            content: "";
            display: block;
            clear: both;
        }

        #btn {
            display: block;
            font-size: 1.5rem;
            padding: 5px;
            margin: 0 auto;
            cursor: pointer;
        }

        .panel {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            padding: 10px;
            width: 300px;
            border: 1px solid;
            border-radius: 5px;
            background: #fff;
            display: none;
            z-index: 2;
        }

        .panel .close {
            float: right;
            margin-top: 15px;
            margin-right: 15px;
            font-size: 1.15rem;
            cursor: pointer;
        }

        .panel .sure,
        .panel .cancel {
            border: 1px solid;
            border-radius: 5px;
            margin: 5px;
            padding: 5px;
            float: right;
            cursor: pointer;
        }

        .shadow {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: #ccc;
            opacity: 0.75;
            z-index: 1;
            display: none;
        }
    </style>
</head>

<body>
    <button id="btn">點(diǎn)我跳出模態(tài)框</button>
    <div class="panel clearfix">
        <div class="close">X</div>
        <h1>我是一級(jí)標(biāo)題</h1>
        <p>我是一大段文字</p>
        <div class="cancel">取消</div>
        <div class="sure">確定</div>

    </div>
    <div class="shadow"></div>
    <script>
        function $(str) {
            return document.querySelector(str);
        }

        var panel = $(".panel");
        var btn = $("#btn");
        var close = $(".panel>.close")
        var shadow = $(".shadow")

        btn.addEventListener("click", function(e) {
            e.stopPropagation();
            panel.style.display = "block";
            shadow.style.display = "block";
        })

        close.addEventListener("click", function() {
            panel.style.display = "none";
            shadow.style.display = "none";
        })

        panel.addEventListener("click", function(e) {
            e.stopPropagation();
        })

        window.addEventListener("click", function() {
            panel.style.display = "none";
            shadow.style.display = "none";
        })
    </script>
</body>

</html>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容