/**
* v-dialogDrag 弹窗拖拽
* Copyright (c) 2019 ruoyi
*/
export default {
bind(el, binding, vnode, oldVnode) {
const value = binding.value
if (value == false) return
// èŽ·å–æ‹–拽内容头部
const dialogHeaderEl = el.querySelector('.el-dialog__header');
const dragDom = el.querySelector('.el-dialog');
dialogHeaderEl.style.cursor = 'move';
// 获å–原有属性 ie domå…ƒç´ .currentStyle ç«ç‹è°·æŒ window.getComputedStyle(domå…ƒç´ , null);
const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null);
dragDom.style.position = 'absolute';
dragDom.style.marginTop = 0;
let width = dragDom.style.width;
if (width.includes('%')) {
width = +document.body.clientWidth * (+width.replace(/\%/g, '') / 100);
} else {
width = +width.replace(/\px/g, '');
}
dragDom.style.left = `${(document.body.clientWidth - width) / 2}px`;
// é¼ æ ‡æŒ‰ä¸‹äº‹ä»¶
dialogHeaderEl.onmousedown = (e) => {
// é¼ æ ‡æŒ‰ä¸‹ï¼Œè®¡ç®—å½“å‰å…ƒç´ è·ç¦»å¯è§†åŒºçš„è·ç¦» (é¼ æ ‡ç‚¹å‡»ä½ç½®è·ç¦»å¯è§†çª—å£çš„è·ç¦»)
const disX = e.clientX - dialogHeaderEl.offsetLeft;
const disY = e.clientY - dialogHeaderEl.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;
let finallyL = l + styL
let finallyT = t + styT
// 移动当å‰å…ƒç´
dragDom.style.left = `${finallyL}px`;
dragDom.style.top = `${finallyT}px`;
};
document.onmouseup = function (e) {
document.onmousemove = null;
document.onmouseup = null;
};
}
}
};