const dragDialog = { bind: function (el: any, binding: any) { el.getElementsByClassName("ant-modal-header")[0].addEventListener("mousedown", (e: any) => { return dragDialog.onMousedown(el, e); }); }, update(el: any, binding: any, vnode: any, old: any) { // 实现每次打开 Dialog 都复位到中央 setTimeout(() => { if (el.style.display === "none") { const elDialog = el.getElementsByClassName("ant-modal-content")[0]; elDialog.style.top = ""; elDialog.style.left = ""; } }, 333); // Dialog 的关闭动画用时 300 毫秒,因此这里需要延迟执行,而且要多延迟一点点 }, /** * @Description: 监听鼠标按压事件 * @author LuXM * @date 2021/8/30 * @time 17:25 */ onMousedown(el: any, e: any): boolean { const elDialog = el.getElementsByClassName("ant-modal-content")[0]; const initClientX = e.clientX; const initClientY = e.clientY; const initStyleLeft = elDialog.style.left ? elDialog.style.left.replace("px", "") - 0 : 0; const initStyleTop = elDialog.style.top ? elDialog.style.top.replace("px", "") - 0 : 0; document.onmousemove = function (e) { const moveX = e.clientX - initClientX + initStyleLeft; const moveY = e.clientY - initClientY + initStyleTop; elDialog.style.left = moveX + "px"; elDialog.style.top = moveY + "px"; }; document.onmouseup = function (e) { document.onmousemove = null; document.onmouseup = null; }; // 不加return false的话可能导致黏连,就是拖到一个地方时div粘在鼠标上不下来,相当于onmouseup失效 return false; }, }; /** * @description: a-modal实现可拖拽功能 *@使用说明:直接在a-modal 标签中加v-drag-dialog指令 * @author LuXM * @date 2021/3/30 11:27 * @update LiuBo 微调改造 2021年8月30日14:46:12 */ export default dragDialog;