html結構:css
<input type="button" id="btn" value="測試modal">複製代碼
css樣式:html
body{
margin: 0;
padding: 0;
}
.mask {
width: 100%;
height: 100%;
position: fixed;
background: #000;
opacity: .5;
}
.modal {
width: 300px;
background: #fff;
position: fixed;
overflow: auto;
left: 50%;
top: 50%;
z-index: 100;
border-radius: 4px;
box-shadow:0 2px 8px rgba(0,0,0,.2);
}
.modal-header {
padding: 13px 16px;
border-radius: 4px 4px 0 0;
background: #fff;
color: rgba(0,0,0,.65);
border-bottom: 1px solid #e9e9e9;
}
.modal-body{
padding: 16px;
font-size: 12px;
line-height: 1.5;
}
.modal-footer {
border-top: 1px solid #e9e9e9;
padding: 10px 16px 10px 10px;
text-align: right;
border-radius: 0 0 4px 4px;
}
.modal-btn {
display: inline-block;
margin-bottom: 0;
font-weight: 500;
text-align: center;
cursor: pointer;
background-image: none;
border: 1px solid transparent;
white-space: nowrap;
line-height: 1.15;
padding: 0 15px;
font-size: 12px;
border-radius: 4px;
height: 28px;
user-select: none;
position: relative;
background-color: #fff;
border-color: #d9d9d9;
}
.modal-btn, .modal-btn:active, .modal-btn:focus {
outline: 0;
}
.modal-btn-primary {
color: #fff;
background-color: #108ee9;
border-color: #108ee9;
}
.modal-close {
cursor: pointer;
border: 0;
background: transparent;
position: absolute;
right: 0;
top: 0;
z-index: 10;
font-weight: 700;
line-height: 1;
text-decoration: none;
color: rgba(0,0,0,.43);
outline: 0;
width: 48px;
height: 48px;
line-height: 48px;
}
複製代碼
js交互邏輯:瀏覽器
//面向對象拖拽
function Drag(){
this.disX = 0;
this.disY = 0;
};
Drag.prototype.init = function (element){
this.element = element;
var that = this;
this.element.onmousedown = this.downFn.bind(this)
};
Drag.prototype.downFn = function (ev){
this.disX = ev.clientX - this.element.offsetLeft;
this.disY = ev.clientY - this.element.offsetTop;
document.onmousemove = this.moveFn.bind(this);
document.onmouseup = this.upFn.bind(this);
};
Drag.prototype.moveFn = function (ev){
this.element.style.left = ev.clientX - this.disX + 'px';
this.element.style.top = ev.clientY - this.disY + 'px';
};
Drag.prototype.upFn = function (ev){
document.onmousemove = null;
};
/*寬 width
高 height
left值 left
top值 top
肯定觸發函數 okFn
取消觸發函數 cancelFn
是否拖拽 isDrag
內容 */ content
class Modal{
constructor(options){
//默認的配置項 定製配置項
this.defaults={
width:400,
isDrag:false,
title:'這是標題',
content:'這是內容部分',
okFn:null,
cancelFn:function(){}
};
//把參數的第二個對象合併到第一個對象上
Object.assign(this.defaults,options);
//console.log(this.defaults);
console.log(options);
};
//初始化
init(){
this.modalBox=this.createModalHtml();
document.body.appendChild(this.modalBox);
this.maskBox=this.createMaskHtml();
document.body.appendChild(this.maskBox);
this.setStyle(); //設置樣式
window.onresize=this.onResize.bind(this);
this.addEvent();
//拖拽操做:
if(this.defaults.isDrag){
var d=new Drag();
d.init(this.modalBox);
}
};
createModalHtml(){
var div=document.createElement('div');
div.className='modal';
var html=`
<button class="modal-close">X</button>
<div class="modal-header">
${this.defaults.title}
</div>
<div class="modal-body">
${this.defaults.content}
</div>
<div class="modal-footer">
<button class="modal-btn cancel">
<span>取消</span>
</button>
<button class="modal-btn modal-btn-primary ok">
<span>肯定</span>
</button>
</div>
`;
div.innerHTML=html;
return div;
};
//關閉模態框事件
addEvent(){
//保存實例對象
var that=this;
var close=this.modalBox.querySelector('.modal-close');
var ok=this.modalBox.querySelector('.ok');
var cancel=this.modalBox.querySelector('.cancel');
close.onclick=function(){
that.modalBox.remove();
that.maskBox.remove();
};
ok.onclick=function(){
that.modalBox.remove();
that.maskBox.remove();
typeof that.defaults.okFn==='function' && that.defaults.okFn();
};
cancel.onclick=function(){
that.modalBox.remove();
that.maskBox.remove();
typeof that.defaults.cancelFn==='function' && that.defaults.cancelFn();
};
}
//設置樣式
setStyle(){
this.modalBox.style.width=this.defaults.width+'px';
if('height' in this.defaults){
this.modalBox.style.height=this.defaults.height+'px';
};
//若是this.defaults有left屬性和top屬性,那說明配置了left和top,按照配置的來,沒配置就按照默認的來
if('left' in this.defaults){
this.modalBox.style.left=this.defaults.left+'px';
}else{
this.modalBox.style.left=(document.documentElement.clientWidth-this.modalBox.clientWidth)*0.5+'px';
};
if('top' in this.defaults){
this.modalBox.style.top=this.defaults.top+'px';
}else{
this.modalBox.style.top=(document.documentElement.clientHeight-this.modalBox.clientHeight)*0.5+'px';
}
};
//瀏覽器窗口大小發生改變時
onResize(){
this.setStyle();
};
//建立黑色背景遮罩層
createMaskHtml(){
var div=document.createElement('div');
div.className='mask';
return div;
};
}
var btn=document.getElementById('btn');
btn.onclick=function(){
//初始化一個彈框
var m=new Modal(
{
width:500,
isDrag:true,
title:'傳入的標題:Hello',
okFn:function(){
console.log("我點擊了肯定按鈕,我要作一些事情");
},
cancelFn:function(){
console.log("我點擊了取消按鈕,我要作一些事情");
},
content:'<p>傳入的內容:今年的春天有點兒長啊!</p><p>傳入的內容:今年的春天有點兒長啊!</p><p>傳入的內容:今年的春天有點兒長啊!</p><p>傳入的內容:今年的春天有點兒長啊!</p><p>傳入的內容:今年的春天有點兒長啊!</p>'
}
);
m.init();
};
複製代碼