This commit is contained in:
xd 2024-07-31 09:51:18 +08:00
parent 105d18a95e
commit 67e32dbff7
1 changed files with 94 additions and 107 deletions

View File

@ -1,123 +1,110 @@
export default { export default {
bind(el, binding, vnode, oldVnode) { bind (el, binding, vnode, oldVnode) {
let resizeEvent = new CustomEvent('drag-resize',{detail:'尺寸变化',bubbles:false}); // 弹框可拉伸最小宽高
//初始化不最大化 // let minWidth = 400
el.fullscreen = false; // let minHeight = 300
//弹框可拉伸最小宽高 // 初始非全屏
let minWidth = 400; let isFullScreen = false
let minHeight = 300; // 当前宽高
//当前宽高 let nowWidth = 0
let nowWidth = minWidth; let nowHight = 0
let nowHight = minHeight; // 当前顶部高度
//当前顶部高度 let nowMarginTop = 0
let nowMarginTop = 0; // 获取弹框头部(这部分可双击全屏)
//获取弹框头部(这部分可双击全屏) const dialogHeaderEl = el.querySelector('.el-dialog__header')
const dialogHeaderEl = el.querySelector('.el-dialog__header'); let hasSetBodyHight = false
let hasSetBodyHight = false; // 弹窗
//弹窗 const dragDom = el.querySelector('.el-dialog')
const dragDom = el.querySelector('.el-dialog'); // 给弹窗加上overflow auto不然缩小时框内的标签可能超出dialog
dragDom.className += ' el-drag-dialog'; // dragDom.style.overflow = "auto"
//清除选择头部文字效果 // 清除选择头部文字效果
dialogHeaderEl.onselectstart = new Function("return false"); dialogHeaderEl.onselectstart = new Function('return false')
//头部加上可拖动cursor // 头部加上可拖动cursor
dialogHeaderEl.style.cursor = 'move'; dialogHeaderEl.style.cursor = 'move'
// 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null)
// 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null); const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null)
const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null); // 头部插入最大化最小化元素
let maxMin = document.createElement('button')
//头部插入最大化最小化元素 maxMin.className += ' el-dialog__headerbtn el-dialog__minmax'
let maxMin = document.createElement("button"); maxMin.style.right = '40px'
maxMin.className += ' el-dialog__headerbtn el-dialog__minmax'; maxMin.style.color = '#909399'
maxMin.style.right = '40px'; maxMin.title = '最大化'
maxMin.style.color = '#909399'; maxMin.innerHTML = '<i class="el-icon-full-screen" onMouseOver="this.style.color=\'#409EFF\'" onMouseOut="this.style.color=\'inherit\'"></i>'
maxMin.title = el.fullscreen ? '还原' : '最大化'; dialogHeaderEl.insertBefore(maxMin, dialogHeaderEl.childNodes[1])
maxMin.innerHTML = '<i class=' + (el.fullscreen ? '"el-icon-crop"' : '"el-icon-full-screen"') + ' onMouseOver="this.style.color=\'#409EFF\'" onMouseOut="this.style.color=\'inherit\'"></i>';
dialogHeaderEl.insertBefore(maxMin, dialogHeaderEl.childNodes[1]);
let moveDown = (e) => { let moveDown = (e) => {
// 鼠标按下,计算当前元素距离可视区的距离 // 鼠标按下,计算当前元素距离可视区的距离
const disX = e.clientX - dialogHeaderEl.offsetLeft; const disX = e.clientX - dialogHeaderEl.offsetLeft
const disY = e.clientY - dialogHeaderEl.offsetTop; const disY = e.clientY - dialogHeaderEl.offsetTop
// 获取到的值带px 正则匹配替换 // 获取到的值带px 正则匹配替换
let styL, styT; let styL, styT
// 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
if (sty.left.includes('%')) { if (sty.left.includes('%')) {
styL = +document.body.clientWidth * (+sty.left.replace(/\%/g, '') / 100); styL = +document.body.clientWidth * (+sty.left.replace(/\%/g, '') / 100)
styT = +document.body.clientHeight * (+sty.top.replace(/\%/g, '') / 100); styT = +document.body.clientHeight * (+sty.top.replace(/\%/g, '') / 100)
} else { } else {
styL = +sty.left.replace(/\px/g, ''); styL = +sty.left.replace(/\px/g, '')
styT = +sty.top.replace(/\px/g, ''); styT = +sty.top.replace(/\px/g, '')
} }
;
document.onmousemove = function (e) { document.onmousemove = function (e) {
// 通过事件委托,计算移动的距离 // 通过事件委托,计算移动的距离
const l = e.clientX - disX; const l = e.clientX - disX
const t = e.clientY - disY; const t = e.clientY - disY
// 移动当前元素 // 移动当前元素
dragDom.style.left = `${l + styL}px`; dragDom.style.left = `${l + styL}px`
dragDom.style.top = `${t + styT}px`; dragDom.style.top = `${t + styT}px`
// 将此时的位置传出去
//将此时的位置传出去 // binding.value({x:e.pageX,y:e.pageY})
//binding.value({x:e.pageX,y:e.pageY}) }
};
document.onmouseup = function (e) { document.onmouseup = function (e) {
document.onmousemove = null; document.onmousemove = null
document.onmouseup = null; document.onmouseup = null
};
} }
dialogHeaderEl.onmousedown = moveDown; }
let bodyHeight = 'auto'; dialogHeaderEl.onmousedown = moveDown
let bodyHeight = 'auto'
function setMaxMin() { function setMaxMin () {
if (el.fullscreen) { if (isFullScreen == false) {
let i = maxMin.querySelector('.el-icon-crop'); let i = maxMin.querySelector('.el-icon-full-screen')
i.classList.remove('el-icon-crop'); i.classList.remove('el-icon-full-screen')
i.classList.add('el-icon-full-screen'); i.classList.add('el-icon-crop')
maxMin.innerHTML = '<i class="el-icon-full-screen"></i>'; maxMin.title = '还原'
maxMin.title = '最大化'; bodyHeight = dragDom.querySelector('.el-dialog__body').offsetHeight + 'px'
dragDom.style.height = "auto"; nowHight = dragDom.clientHeight
dragDom.style.width = nowWidth + 'px'; nowWidth = dragDom.clientWidth
dragDom.style.marginTop = nowMarginTop; nowMarginTop = dragDom.style.marginTop
el.fullscreen = false; dragDom.style.left = 0
dialogHeaderEl.style.cursor = 'move'; dragDom.style.top = 0
dialogHeaderEl.onmousedown = moveDown; dragDom.style.height = '94VH'
dragDom.querySelector('.el-dialog__body').style.height = bodyHeight; dragDom.style.width = '100VW'
dragDom.querySelector('.el-dialog__body').style.maxHeight = dragDom.style.height dragDom.style.marginTop = 0
hasSetBodyHight = false; isFullScreen = true
} else { dialogHeaderEl.style.cursor = 'initial'
let i = maxMin.querySelector('.el-icon-full-screen'); dialogHeaderEl.onmousedown = null
i.classList.remove('el-icon-full-screen');
i.classList.add('el-icon-crop');
maxMin.title = '还原';
bodyHeight = dragDom.querySelector('.el-dialog__body').offsetHeight + 'px';
nowHight = dragDom.clientHeight;
nowWidth = dragDom.clientWidth;
nowMarginTop = dragDom.style.marginTop;
dragDom.style.left = 0;
dragDom.style.top = 0;
dragDom.style.height = "94VH";
dragDom.style.width = "100VW";
dragDom.style.marginTop = 0;
el.fullscreen = true;
dialogHeaderEl.style.cursor = 'initial';
dialogHeaderEl.onmousedown = null;
if (!hasSetBodyHight) { if (!hasSetBodyHight) {
let footerHeight = dragDom.querySelector('.el-dialog__footer') && dragDom.querySelector('.el-dialog__footer').offsetHeight; dragDom.querySelector('.el-dialog__body').style.height = 'calc(100% - 130px)'
dragDom.querySelector('.el-dialog__body').style.height = 'calc(100% - 110px)';
dragDom.querySelector('.el-dialog__body').style.maxHeight = dragDom.style.height dragDom.querySelector('.el-dialog__body').style.maxHeight = dragDom.style.height
hasSetBodyHight = true; hasSetBodyHight = true
}
} else {
let i = maxMin.querySelector('.el-icon-crop')
i.classList.remove('el-icon-crop')
i.classList.add('el-icon-full-screen')
maxMin.innerHTML = '<i class="el-icon-full-screen"></i>'
maxMin.title = '最大化'
dragDom.style.height = 'auto'
dragDom.style.width = nowWidth + 'px'
dragDom.style.marginTop = nowMarginTop
isFullScreen = false
dialogHeaderEl.style.cursor = 'move'
dialogHeaderEl.onmousedown = moveDown
dragDom.querySelector('.el-dialog__body').style.height = bodyHeight
hasSetBodyHight = false
} }
} }
el.dispatchEvent(resizeEvent); // 点击放大缩小效果
} maxMin.onclick = setMaxMin
// 双击头部效果
//点击放大缩小效果 dialogHeaderEl.ondblclick = setMaxMin
maxMin.onclick = setMaxMin;
//双击头部效果
dialogHeaderEl.ondblclick = setMaxMin;
} }
} }