JavaScript案例:div对话框

<!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>Document</title>
    <style>
        .window{
            width: 300px;
            margin: 0 auto;
            border: 1px solid #999;
            background-color: #fff;
            display: none;
            position:absolute;
            z-index: 100;
        }
        .windowBg{
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: black;
            opacity: 0.3;
            display: none;
        }
        .win-title{
            height: 50px;
            line-height: 50px;
            padding: 0 10px;
            border-bottom: 1px solid #c5c5c5;
            color: #333;
            border-radius: 2px 2px 0 0;
        }
        .win-info{
            padding: 20px;
            line-height: 24px;
        }
        .win-bottom{
            text-align: right;
            padding: 0 15px 10px 0px;
        }
        .win-btn{
            display: inline-block;
            border-color: #1E9FFF;
            background-color: #1E9FFF;
            color: #fff;
            width: 60px;
            height: 30px;
            text-align: center;
        }
    </style>
    <script>
        window.onload = function(){
            document.getElementById("btn").onclick = function(e){
                var win = document.getElementById("windowBox")
                win.style.display = "block"
                console.log(win.clientWidth)
                console.log(win.clientHeight)
                var winBg = document.getElementById("windowBg")
                winBg.style.display = "block"
                win.style.top = (window.innerHeight - win.clientHeight )/2+"px"
                win.style.left = (window.innerWidth - win.clientWidth )/2+"px"
            }
            // document.getElementById("windowBg").onclick = function(e){
            //     closeWin()
            // }
            document.getElementById("0").onclick = function(e){
                closeWin()
            }
            document.getElementById("1").onclick = function(e){
                closeWin()
            }
            function closeWin(){
                var win = document.getElementById("windowBox")
                win.style.display = "none"
                var winBg = document.getElementById("windowBg")
                winBg.style.display = "none"
            }
        }


    </script>
</head>
<body>
    <input type="button" value="按钮" id="btn">
    <div id="windowBox" class="window">
        <div class="win-title">提示</div>
        <div class="win-info">您确认删除数据吗?</div>
        <div class="win-bottom">
            <button class="win-btn" id="0">是</button>
            <button class="win-btn" id="1">否</button>
        </div>
    </div>
    <div id="windowBg" class="windowBg"></div>
</body>
</html>

原文出处:https://malaoshi.top/show_1IX3njKo6MoV.html