最近有一个需求是想要实现ant框架的弹窗可以做到拉伸放大缩小,去网上百度,大多数是el-dialog的,没有找到ant-modal的,然后我去ant群问有没有人做过得到回复是element框架的,实践了一下,发现element的弹窗跟ant框架有冲突,ant的多选框完全点击不了,遂放弃,想要照用element的代码代入到ant-modal里面去,可以实现得了;希望大家就不要遇到跟我一样的问题,我是莱不莱多小菜鸡;
废话不多说,下面直接贴代码
先建一个js文件
import Vue from 'vue'
const move = Vue.directive('move',{inserted:(el, bindings, vnode)=>{
Vue.nextTick(() => {
let { visible,destroyOnClose } = vnode.componentInstance
if (!visible) return
let modal = el.getElementsByClassName('ant-modal')[0]
// console.log(modal);
let header = el.getElementsByClassName('role')[0]
console.log(el.getElementsByClassName('ant-spin-nested-loading')[0]);
//弹框可拉伸最小宽高
let minWidth = 400;
let minHeight = 300;
//初始非全屏
let isFullScreen = false;
// //当前宽高
let nowWidth = 0;
let nowHight = 0;
// //当前顶部高度
let nowMarginTop = 0;
//获取弹框头部(这部分可双击全屏)
// const header = el.querySelector('.el-dialog__header');
// const header = el.querySelector('.el-dialog__header');
// //弹窗
// // const modal = el.querySelector('.el-dialog');
// const modal = el.querySelector('.ant-modal');
// let contentDom = document.querySelector('.content');
// const bottomDom = el.querySelector('.child')
// 全屏按钮
header.insertAdjacentHTML('beforeEnd', '<div class="bigbtn"><div class="changeBig"></div><div class="changeSmall"></div></div>');
const bigBtn = el.querySelector('.bigbtn');
const changeBig = el.querySelector('.changeBig');
const changeSmall = el.querySelector('.changeSmall');
//给弹窗加上overflow auto;不然缩小时框内的标签可能超出dialog;
modal.style.overflow = "auto";
//清除选择头部文字效果
//header.onselectstart = new Function("return false");
//头部加上可拖动cursor
header.style.cursor = 'move';
// 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
const sty = modal.currentStyle || window.getComputedStyle(modal, null);
let moveDown = (e) => {
// 鼠标按下,计算当前元素距离可视区的距离
const disX = e.clientX - header.offsetLeft;
const disY = e.clientY - header.offsetTop;
// 获取到的值带px 正则匹配替换
let styL, styT;
// 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
if (sty.left.includes('%')) {
styL = +document.body.clientWidth * (+sty.left.replace(/%/g, '') / 100);
styT = +document.body.clientHeight * (+sty.top.replace(/%/g, '') / 100);
} else {
styL = +sty.left.replace(/\px/g, '');
styT = +sty.top.replace(/\px/g, '');
}
document.onmousemove = function (e) {
// 通过事件委托,计算移动的距离
const l = e.clientX - disX;
const t = e.clientY - disY;
// 移动当前元素
modal.style.left = `${l + styL}px`;
modal.style.top = `${t + styT}px`;
//将此时的位置传出去
//binding.value({x:e.pageX,y:e.pageY})
};
document.onmouseup = function (e) {
document.onmousemove = null;
document.onmouseup = null;
};
}
header.onmousedown = moveDown;
// 双击头部全屏效果
bigBtn.onclick = (e) => {
if (isFullScreen == false) {
changeBig.style.display = 'none';
changeSmall.style.display = 'block';
nowHight = modal.clientHeight;
nowWidth = modal.clientWidth;
nowMarginTop = modal.style.marginTop;
modal.style.left = 0;
modal.style.top = 0;
modal.style.height = "100VH";
modal.style.width = "100VW";
modal.style.marginTop = 0;
isFullScreen = true;
header.style.cursor = 'initial';
header.onmousedown = null;
} else {
changeBig.style.display = 'block';
changeSmall.style.display = 'none';
modal.style.height = "auto";
modal.style.width = nowWidth + 'px';
modal.style.marginTop = nowMarginTop;
isFullScreen = false;
header.style.cursor = 'move';
header.onmousedown = moveDown;
}
}
modal.onmousemove = function (e) {
// console.log(header,modal.querySelector('.el-dialog__body'), 123);
let moveE = e;
if (e.clientX > modal.offsetLeft + modal.clientWidth - 10 || modal.offsetLeft + 10 > e.clientX) {
// console.log(e.clientX > modal.offsetLeft + modal.clientWidth - 10 || modal.offsetLeft + 10 > e.clientX);
modal.style.cursor = 'w-resize';
} else if (el.scrollTop + e.clientY > modal.offsetTop + modal.clientHeight - 10) {
// console.log(el.scrollTop + e.clientY > modal.offsetTop + modal.clientHeight - 10);
modal.style.cursor = 's-resize';
}else{
modal.style.cursor = 'default';
modal.onmousedown = null;
}
modal.onmousedown = (e) => {
const clientX = e.clientX;
const clientY = e.clientY;
let elW = modal.clientWidth;
let elH = modal.clientHeight;
console.log(modal.clientHeight);
let EloffsetLeft = modal.offsetLeft;
let EloffsetTop = modal.offsetTop;
modal.style.userSelect = 'none';
let ELscrollTop = el.scrollTop;
//判断点击的位置是不是为头部
if (clientX > EloffsetLeft && clientX < EloffsetLeft + elW && clientY > EloffsetTop && clientY < EloffsetTop + 100) {
//如果是头部在此就不做任何动作,以上有绑定header.onmousedown = moveDown;
} else {
document.onmousemove = function (e) {
e.preventDefault(); // 移动时禁用默认事件
//左侧鼠标拖拽位置
if (clientX > EloffsetLeft && clientX < EloffsetLeft + 10) {
//往左拖拽
if (clientX > e.clientX) {
modal.style.width = elW + (clientX - e.clientX) * 2 + 'px';
}
//往右拖拽
if (clientX < e.clientX) {
if (!(modal.clientWidth < minWidth)) {
modal.style.width = elW - (e.clientX - clientX) * 2 + 'px';
}
// else {
// modal.style.width = elW - (e.clientX - clientX) * 2 + 'px';
// }
}
}
//右侧鼠标拖拽位置
if (clientX > EloffsetLeft + elW - 10 && clientX < EloffsetLeft + elW) {
//往左拖拽
if (clientX > e.clientX) {
console.log(234);
if (modal.clientWidth < minWidth) {
console.log('test');
// modal.style.width = elW - (clientX - e.clientX) * 2 + 'px';
}
else {
modal.style.width = elW - (clientX - e.clientX) * 2 + 'px';
}
}
//往右拖拽
if (clientX < e.clientX) {
modal.style.width = elW + (e.clientX - clientX) * 2 + 'px';
}
}
//底部鼠标拖拽位置
if (ELscrollTop + clientY > EloffsetTop + elH - 20 && ELscrollTop + clientY < EloffsetTop + elH) {
//往上拖拽
if (clientY > e.clientY) {
if (!(modal.clientHeight < minHeight)) {
modal.style.height = elH - (clientY - e.clientY) * 2 + 'px';
}else {
modal.style.height = elH - (clientY - e.clientY) * 2 + 'px';
}
}
//往下拖拽
if (clientY < e.clientY) {
modal.style.height = elH + (e.clientY - clientY) * 2 + 'px';
}
}
};
//拉伸结束
document.onmouseup = function (e) {
document.onmousemove = null;
document.onmouseup = null;
};
}
}
}
// bottomDom.onmousemove = function(e){
// bottomDom.onmousedown = function(e){
// bottomDom.style.cursor = 'nw-resize';
// console.log(111);
// }
// }
})
}})
export {move}
然后在main.js文件引入
import move from "./utils/move"
运用
在需要用到的弹窗的写上
<a-modal :width="modalWidth" :visible="visible" :maskClosable="false" :centered="true"
:closable="false" :footer="null" @cancel="close" :keyboard="keyboard" v-move>
</a-modal>