edge瀏覽器下作的gif
http://www.lanourteam.com/%E6...
有幾個點需要注意一下
- 每個彈窗都要有唯一dom可操作 指令可以做到
- 拖拽時要添加可拖拽區塊 header
- 由於element-ui dialog組件在設計時寬度用了百分比, 這里不同瀏覽器有兼容性問題
- 實現拖拽寬高時 獲取邊緣問題 div定位 設置模擬邊緣
<template>
<el-dialog
v-dialogDrag
ref="dialog__wrapper">
<div class="dialog-body">
<div
class="line"
v-dialogDragWidth="$refs.dialog__wrapper"></div>
</div>
</el-dialog>
</template>
dialog組件的其它屬性這里就不寫了. 項目中的指令都定義directives.js中集中管理, 全局注冊.
directives.js:
1 import Vue from 'vue'; 2 3 // v-dialogDrag: 彈窗拖拽 4 Vue.directive('dialogDrag', { 5 bind(el, binding, vnode, oldVnode) { 6 const dialogHeaderEl = el.querySelector('.el-dialog__header'); 7 const dragDom = el.querySelector('.el-dialog'); 8 dialogHeaderEl.style.cursor = 'move'; 9 10 // 獲取原有屬性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null); 11 const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null); 12 13 dialogHeaderEl.onmousedown = (e) => { 14 // 鼠標按下,計算當前元素距離可視區的距離 15 const disX = e.clientX - dialogHeaderEl.offsetLeft; 16 const disY = e.clientY - dialogHeaderEl.offsetTop; 17 18 // 獲取到的值帶px 正則匹配替換 19 let styL, styT; 20 21 // 注意在ie中 第一次獲取到的值為組件自帶50% 移動之后賦值為px 22 if(sty.left.includes('%')) { 23 styL = +document.body.clientWidth * (+sty.left.replace(/\%/g, '') / 100); 24 styT = +document.body.clientHeight * (+sty.top.replace(/\%/g, '') / 100); 25 }else { 26 styL = +sty.left.replace(/\px/g, ''); 27 styT = +sty.top.replace(/\px/g, ''); 28 }; 29 30 document.onmousemove = function (e) { 31 // 通過事件委托,計算移動的距離 32 const l = e.clientX - disX; 33 const t = e.clientY - disY; 34 35 // 移動當前元素 36 dragDom.style.left = `${l + styL}px`; 37 dragDom.style.top = `${t + styT}px`; 38 39 //將此時的位置傳出去 40 //binding.value({x:e.pageX,y:e.pageY}) 41 }; 42 43 document.onmouseup = function (e) { 44 document.onmousemove = null; 45 document.onmouseup = null; 46 }; 47 } 48 } 49 }) 50 51 // v-dialogDragWidth: 彈窗寬度拖大 拖小 52 Vue.directive('dialogDragWidth', { 53 bind(el, binding, vnode, oldVnode) { 54 const dragDom = binding.value.$el.querySelector('.el-dialog'); 55 56 el.onmousedown = (e) => { 57 58 // 鼠標按下,計算當前元素距離可視區的距離 59 const disX = e.clientX - el.offsetLeft; 60 61 document.onmousemove = function (e) { 62 e.preventDefault(); // 移動時禁用默認事件 63 64 // 通過事件委托,計算移動的距離 65 const l = e.clientX - disX; 66 dragDom.style.width = `${l}px`; 67 }; 68 69 document.onmouseup = function (e) { 70 document.onmousemove = null; 71 document.onmouseup = null; 72 }; 73 } 74 } 75 })
main.js:
1 // 引入自定義指令 2 import './directives.js';
