在utils中新建drag.js文件
import Vue from 'vue'
Vue.directive('dragModal', {
bind(el, binding, vnode, oldVnode) {
const dialogHeaderEl = el.querySelector('.ant-modal-header')
const dragDom = el.querySelector('.ant-modal')
dialogHeaderEl.style.cursor = 'move'
const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null)
dialogHeaderEl.onmousedown = (e) => {
const disX = e.clientX - dialogHeaderEl.offsetLeft
const disY = e.clientY - dialogHeaderEl.offsetTop
let styL, styT
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
dragDom.style.left = `${l + styL}px`
dragDom.style.top = `${t + styT}px`
}
document.onmouseup = function (e) {
document.onmousemove = null
document.onmouseup = null
}
}
}
})
在main.js文件中引入
import '@/utils/drag'
使用到实例中:v-dragModalt
<a-modal v-model:visible="visible" title="Basic Modal" @ok="handleOk" v-dragModalt></a-modal>