// 遮罩
export class Mask {
// 层数,$vm(this),控制弹窗显示的值
constructor(zIndex = 999, that = null, visible ='') {
this.zIndex = zIndex;
this.mask = document.createElement('div')
this.mask.style.cssText = `position: fixed; top: 0; left: 0; right: 0; bottom: 0; background-color: rgba(0,0,0,0.3); z-index: ${this.zIndex};`;
this.mask.addEventListener('click', function() {
if (visible) {
that[visible] = false
}
})
}
show() {
document.body.appendChild(this.mask);
document.body.style.overflow = 'hidden';
}
remove() {
this.mask.remove();
document.body.style.overflow = '';
}
}
使用
const mask = new Mask(99999,$vm,'visible')
mask.show() // 显示遮罩
mask.remove()