封装弹窗并模拟confirm方法

代码如下

<!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>
        .cancel,
        .confirm {
            cursor: pointer;
            width: 40%;
            outline: none;
            border: 0px;
            text-align: center;
            background: white;
            color: blue;
            border: 1px solid lightseagreen;
        }

        .cancel {
            margin-right: 10%;
        }

        .confirm {
            margin-left: 10%;
        }
    </style>
</head>

<body>
    <button>弹出中间弹框</button>
</body>
<script>
    var oButton = document.getElementsByTagName("button")[0];
    oButton.onclick = function () {
        new layer(
            { layerCertain: true, position: 'left', top: '200px', left: '350px' }, function () {
                alert('点击了确认')
            }, function () {
                alert('点击了取消')
            });
    }
    function layer(styleChange, funOne, funTwo) {
        this.funOne = funOne||null;
        this.funTwo = funTwo||null;
        this.style = {
            title: '系统提示',
            content: '这是默认的提示',
            position: 'center',
            layerCertain: false,
        }
        if (styleChange != 'undefined') {
            this.forChangeStyle(styleChange);
        }
        this.init();
    }
    layer.prototype = {
        init() {
            this.createDiv();
            if (this.style.layerCertain) {
                this.layerCertain();
            }
            this.closeLayer();
            this.layerConfirm(this.funOne);
            this.layerCancel(this.funTwo);
        },
        createDiv() {
            var iframe = document.createElement('div');
            var style = document.createElement("style");
            var classStyle = '';
            if (this.style.position == 'center') {
                classStyle = '.layerPosition{position:fixed;z-index:11;background:lightpink;display:inline-block;height:105px;width:200px;top:0;left:0;bottom:0;right:0;margin:auto';
            }
            else if (this.style.position == 'left') {
                classStyle = '.layerPosition{position:fixed;z-index:11;background:lightpink;display:inline-block;height:105px;width:200px;top:' + this.style.top + ';left:' + this.style.left + '';
            }
            iframe.className = 'layerPosition';
            style.appendChild(document.createTextNode(classStyle));
            var head = document.getElementsByTagName("head")[0];
            head.appendChild(style);
            iframe.innerHTML = '<h3>' + this.style.title + '<p style="color:red;cursor:pointer;margin:0;float:right"id="closeLayer">XXX</p></h3><div>' + this.style.content + '</div><div><button class="cancel">取消</button><button class="confirm">确定</button>';
            document.body.appendChild(iframe);
        },
        layerCertain() {
            var oCertain = document.createElement('div');
            var style = document.createElement("style");
            var classStyle = '';
            classStyle = '.layerCertain{position:fixed;z-index:1;top:0;left:0;bottom:0;right:0;background:rgba(0,0,0,0.4)}';
            style.appendChild(document.createTextNode(classStyle));
            var head = document.getElementsByTagName("head")[0];
            head.appendChild(style);
            oCertain.className = 'layerCertain';
            document.body.appendChild(oCertain);
        },
        closeLayer() {
            this.addHandler(document.getElementById('closeLayer'), 'click', function () {
                var certain = document.getElementsByClassName('layerCertain')[0] || null,
                    layerPanel = document.getElementsByClassName('layerPosition')[0];
                if (certain) {
                    certain.parentElement.removeChild(certain);
                }
                layerPanel.parentElement.removeChild(layerPanel);
            });
        },
        //这个是回调的重点
        layerConfirm(funOne) {
            var _this = this;
            this.addHandler(document.getElementsByClassName('confirm')[0], 'click', function () {
                if (funOne && typeof funOne == 'function') {
                    funOne();
                }
                var certain = document.getElementsByClassName('layerCertain')[0] || null,
                    layerPanel = document.getElementsByClassName('layerPosition')[0];
                if (certain) {
                    certain.parentElement.removeChild(certain);
                }
                layerPanel.parentElement.removeChild(layerPanel);
            })
        },
        //这个是回调的重点
        layerCancel(funTwo){
            var _this = this;
            this.addHandler(document.getElementsByClassName('cancel')[0],'click',function(){
                if(funTwo&&typeof funTwo == 'function'){
                    funTwo();
                }
                var certain = document.getElementsByClassName('layerCertain')[0] || null,
                    layerPanel = document.getElementsByClassName('layerPosition')[0];
                if (certain) {
                    certain.parentElement.removeChild(certain);
                }
                layerPanel.parentElement.removeChild(layerPanel);
            })
        },
        addHandler(obj, type, fun) {
            obj.addEventListener ? obj.addEventListener(type, fun) : obj['on' + type] = fun;
        },
        forChangeStyle(styleChange) {
            for (var key in styleChange) {
                this.style[key] = styleChange[key];
            }
        }

    }
</script>

</html>

效果图如下

这里写图片描述

如有更好的解决方法,欢迎提出~~

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值