封装 模态框 弹框

<!DOCTYPE html>

<html lang="zh">

<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>

    <link rel="stylesheet" href="./model.css">

</head>

<body>

   

    <!-- 需要拷贝以下结构 -->

    <div id="model" style="display: none;">

        <div class="modelDialog">

            <span class="close">x</span>

            <div class="title">

                确认退出

               

            </div>

            <div class="content">

                <div class="subject">确认退出游戏?</div>

                <div class="options">

                    <div class="btn confirm">确认</div>

                    <div class="btn cancle">取消</div>

                </div>

            </div>

        </div>

    </div>

   

   

   

   

    <button>5元购买</button><button>10元购买</button><button>退出游戏</button>

    <script src="./model.js"></script>

    <script>

       

       

       

        var btns = document.querySelectorAll("button");

        btns[0].οnclick=function(){

            // console.log("点击5元");

            showModel("#model","确认购买","确定花费五块钱购买后羿么?", function(){

                console.log("恭喜你, 购买成功");

            }, function(){

                console.log("下次还来找我买呀");

            })

        }

        btns[1].οnclick=function(){

            // console.log("点击10元");

            showModel("#model","确认购买","确定花费10块钱购买精灵王么?", function(){

                console.log("你的钱不够了");

            }, function(){

                setTimeout(function(){

                    showModel("#model","小心呀","过了这个村就没这个店,买吧?", function(){

                        console.log("恭喜你, 购买成功");

                    },function(){

                        console.log("问你两次都不买,真穷");

                    })

                }, 500)

            }, true)

        }

        btns[2].οnclick=function(){

            // console.log("点击退出游戏");

            showModel("#model","确认退出","确定退出游戏么?", function(){

                console.log("关闭");

            }, function(){

                console.log("继续玩");

            })

        }

       

    </script>

   

</body>

</html>

css样式

        #model{
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
            background-color: rgba(0, 0, 0, 0.6);
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 30px;
        }
        
        #model .modelDialog{
            width: 600px;
            height: 400px;
            background-color: aliceblue;
            position: relative;
        }
        
        #model .modelDialog .title{
            height: 60px;
            background-color: lightblue;
            text-align: center;
            line-height: 60px;
            position: relative;
        }
        #model .modelDialog .close{
            display: inline-block;
            width: 40px;
            text-align: center;
            height: 40px;
            line-height: 40px;
            border: 1px solid lightcoral;
            border-radius: 50%;
            position: absolute;
            right: 20px;
            top: 10px;
            z-index: 50;
        }
        
        #model .modelDialog .content{
            height: 360px;
            background-color: lightgray;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: space-around;
            
        }
        #model .modelDialog .content .options{
            width: 80%;
            display: flex;
            justify-content: space-around;
        }
        
        #model .modelDialog .content .options .btn{
            border: 1px solid lightcoral;
            border-radius: 5px;
            padding: 5px 10px;
        }

js样式

function showModel(query,title,subject,confimCallbak,cancelCallBack, showClose=false){      

            var modelEle = document.querySelector(query);

            modelEle.style.display="flex";

            var titleEle = modelEle.getElementsByClassName("title")[0];

            titleEle.innerText=title;

            var subjectEle = modelEle.getElementsByClassName("subject")[0];

            subjectEle.innerText=subject;

            var confirmEle = modelEle.getElementsByClassName("confirm")[0];

            confirmEle.οnclick=function(){

                confimCallbak()

                modelEle.style.display="none"

               

            }

            var cancleEle = modelEle.getElementsByClassName("cancle")[0];

            cancleEle.οnclick=function(){

                cancelCallBack()

                modelEle.style.display="none"

            }

            var close = modelEle.getElementsByClassName("close")[0];

            close.style.display=showClose?"inline-block":"none"

            close.οnclick=function(){

                cancelCallBack()

                modelEle.style.display="none"

            }

           

        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值