dialog.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import Vue from 'vue'
  2. // v-dialogDrag: 弹窗拖拽
  3. Vue.directive('dialogDrag', {
  4. bind(el, binding, vnode, oldVnode) {
  5. const dialogHeaderEl = el.querySelector('.el-dialog__header')
  6. const dragDom = el.querySelector('.el-dialog')
  7. dialogHeaderEl.style.cursor = 'move'
  8. // 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
  9. const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null)
  10. dialogHeaderEl.onmousedown = (e) => {
  11. // 鼠标按下,计算当前元素距离可视区的距离
  12. const disX = e.clientX - dialogHeaderEl.offsetLeft
  13. const disY = e.clientY - dialogHeaderEl.offsetTop
  14. // 获取到的值带px 正则匹配替换
  15. let styL, styT
  16. // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
  17. if (sty.left.includes('%')) {
  18. styL = +document.body.clientWidth * (+sty.left.replace(/\%/g, '') / 100)
  19. styT = +document.body.clientHeight * (+sty.top.replace(/\%/g, '') / 100)
  20. } else {
  21. styL = +sty.left.replace(/\px/g, '')
  22. styT = +sty.top.replace(/\px/g, '')
  23. }
  24. document.onmousemove = function (e) {
  25. // 通过事件委托,计算移动的距离
  26. const l = e.clientX - disX
  27. const t = e.clientY - disY
  28. // 移动当前元素
  29. dragDom.style.left = `${l + styL}px`
  30. dragDom.style.top = `${t + styT}px`
  31. // 将此时的位置传出去
  32. // binding.value({x:e.pageX,y:e.pageY})
  33. }
  34. document.onmouseup = function (e) {
  35. document.onmousemove = null
  36. document.onmouseup = null
  37. }
  38. }
  39. }
  40. })