基于JavaScript实现模态窗口插件(支持回调)

基于Javascript写的模态窗口

调用接口

Messbox.show({
    title:"标题",          //模态窗口的标题
    content:"内容",        //模态窗口的显示内容,支持html文本
    okText:"确定",         //确定按钮标题,如果不配置默认显示确定
    cancelText:"取消",     //取消按钮标题,如果不配置则不显示取消按钮
    okEvent:function(){},  //确定按钮单击时的执行的事件,如果不配置默认是关闭窗口
});

效果展示

www.iuacc.cn

www.iuacc.cn

www.iuacc.cn

Javascript代码

'use strict';

var Messbox = {};

Messbox.global = this;

//信息框弹窗
Messbox.messageBoxDialog = null;
//信息框标题
Messbox.title = null;
//信息框内容
Messbox.content = null;
//信息框右上角关闭按钮
Messbox.closeBtn = null;
//信息框关闭事件
Messbox.closeEvent = null;
//信息框内容
Messbox.content = null;
//信息框确定按钮
Messbox.okBtn = null;
//确定按钮单击事件
Messbox.okClickEvent = null;
//信息款取消按钮
Messbox.cancelBtn = null;



/**
 * 创建Dom
 */
Messbox.createDom = function (tagName, className) {
	
	var container = document.createElement(tagName); 
	container.setAttribute('class', className);  
	return  container;
    
};

/**
 *创建
 */
Messbox.create = function () {
    Messbox.messageBoxDialog = Messbox.createDom('div', 'message-box-dialog');
	
    let messageBox = Messbox.createDom('div', 'message-box');

    Messbox.title = Messbox.createDom('div', 'message-box-title');
    Messbox.content = Messbox.createDom('div', 'message-box-content');
    Messbox.closeBtn = Messbox.createDom('div', 'message-box-close-btn');
    Messbox.closeBtn.innerHTML = '×';
	
    Messbox.okBtn = Messbox.createDom('div', 'message-box-btn btn-ok');

    Messbox.cancelBtn = Messbox.createDom('div', 'message-box-btn btn-cancel');

    /*给关闭按钮绑定事件*/
    Messbox.bindEvent(Messbox.closeBtn, 'click', function (){
        Messbox.close();
    });

    /*给关闭按钮绑定事件*/
    Messbox.bindEvent(Messbox.cancelBtn, 'click', function (){
        Messbox.close();
    });

    messageBox.appendChild(Messbox.closeBtn);
    messageBox.appendChild(Messbox.title);
    messageBox.appendChild(Messbox.content);
    messageBox.appendChild(Messbox.okBtn);
    messageBox.appendChild(Messbox.cancelBtn);
    Messbox.messageBoxDialog.appendChild(messageBox);
	
    document.body.appendChild(Messbox.messageBoxDialog);
};




/**
 * 绑定事件
 */
Messbox.bindEvent = function (container, type, event) {

    if (typeof event != 'function') {
        return;
    }
    container.addEventListener(type, event, false);
}

/**
 * 移除事件
 */
Messbox.removeEvent = function (container, type, event) {

    if(container == null){
        return ;
    }
    if (typeof event === "function") {
        container.removeEventListener(type, event, false);
    }
}


/**
 * 关闭窗口
 */
Messbox.close = function () {
    Messbox.messageBoxDialog.style.display = "none";
}

/**
 * 显示窗口
 */
Messbox.show = function (opt) {

    if(opt == null){
        return ;
    }

    //设置标题
    if (opt.title === undefined) {
        opt.title = '提示';
    }
    Messbox.title.innerHTML = opt.title;

    //设置内容
    if (opt.content === undefined) {
        opt.content = ''
    }
    Messbox.content.innerHTML = opt.content;

    //设置确定按钮标题
    if (opt.okText === undefined) {
        opt.okText = '确定';
    }
    Messbox.okBtn.innerHTML = opt.okText;

    //确定按钮事件处理
    Messbox.removeEvent(Messbox.okBtn, 'click', Messbox.okClickEvent);//一定要移除,否则会累积绑定
    //配置了确定按钮点击方法
    if (typeof opt.okEvent === 'function') {
        //绑定了事件
        function hasOkEvent(){
            Messbox.close();
			opt.okEvent();
        };

        Messbox.okClickEvent = hasOkEvent;

        Messbox.bindEvent(Messbox.okBtn, 'click', Messbox.okClickEvent);
    }else{

        function hasNotOkEvent(){
            Messbox.close();
        };
        Messbox.okClickEvent = hasNotOkEvent;
        Messbox.bindEvent(Messbox.okBtn, 'click', Messbox.okClickEvent);
    }
	
	//设置取消按钮标题
    if (opt.cancelText === undefined) {
        //没有配置就隐藏按钮
        Messbox.cancelBtn.style.display = "none";
    } else {
        Messbox.cancelBtn.innerHTML = opt.cancelText;
        Messbox.cancelBtn.style.display = "";
    }
	
	//显示
    Messbox.messageBoxDialog.style.display = 'block';
}

Messbox.create();

CSS代码

@keyframes fade-in {
    0% {
        opacity: 0;
    }
    40% {
        opacity: 0;
    }
    100% {
        opacity: 1;
    }
}

.message-box-dialog {
    position: fixed;
    top: 0;
    width: 100%;
    width: 100%;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    z-index: 99999;
    animation: fade-in;
    background: rgba(0, 0, 0, 0.2);
    animation-duration: 0.3s;
    display: none;
}


.message-box {
    width: 400px;
    background: #fff;
    font-family: "SourceHanSansCN Regular";
    border-radius: 10px;
    padding: 20px;
    box-sizing: border-box;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
}

.message-box-close-btn {
    background-color: white;
    position: absolute;
    width: 22px;
    height: 22px;
    background-color: #c7c7c7;
	color: white;
    border-radius: 50%;
    right: 10px;
    top: 10px;
    font-size: 14px;
    transition-duration: 0.2s;
    cursor: pointer;
}

.message-box-close-btn:hover {
    background-color: #008cff;
}

.message-box-title {
    width: 100%;
    height: 40px;
    text-align: center;
    line-height: 50px;
    color: #444444;
    font-size: 26px;
}

.message-box-content {
    width: 100%;
    font-size: 22px;
    margin: 20px 0px 20px 0px;
    color: gray;
}

.message-box-btn {
    border: none;
    color: white;
    padding: 10px 30px;
    margin: 15px 10px 20px 10px;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    cursor: pointer;
    border-radius: 5px;
    background: #dddddd;
}

.btn-ok {
    background-color: #008cff;
}

.btn-ok:hover {
    background-color: #0070f0;
    color: white;
}

.btn-cancel {
    background-color: #dddddd;
}

.btn-cancel:hover {
    background-color: #cccccc;
}

Html代码

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link href="./messbox.css" rel="stylesheet">

</head>
<body>
	<div id="test1" class="message-box-btn btn-ok" onclick="fun1()">按钮1</div>
	<div id="test2" class="message-box-btn btn-ok" onclick="fun2()">按钮2</div>
	<div id="test3" class="message-box-btn btn-ok" onclick="fun3()">按钮3</div>
	
	<script>
		function fun1(){
			Messbox.show({
				title:"我是标题",
				content:"我是内容",
				okText:"确定",
			});
		}
		
		function fun2(){
			Messbox.show({
				title:"提示",
				content:"点击确定按钮执行自定义方法",
				okText:"确定",
				okEvent: function(){
					console.log("确定按钮被单击");
				}
			});
		}
		
		function fun3(){
			Messbox.show({
				title:"提示",
				content:"点击确定按钮执行自定义方法<br><font color='red'>并且显示取消按钮</font>",
				okText:"确定",
				cancelText:"取消",
				okEvent: fun,//当确定按钮点击后,调用下面的fun()
			});
		}
		
		function fun(){
			console.log("确定按钮被单击");
		}
	</script>
	<script type="text/javascript" src="./messbox.js"></script>
</body>

</html>



猜你喜欢

转载自blog.csdn.net/weixin_43532890/article/details/103137100
今日推荐