パッケージモーダルカスタムイベントフレーム+をドラッグアンドドロップするには

HTML構造:

 <input type="button" id="btn" value="测试modal">复制代码

CSSスタイル:

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();
};
  复制代码


ます。https://juejin.im/post/5d038d55f265da1b725bffd4で再現

おすすめ

転載: blog.csdn.net/weixin_33873846/article/details/93169416