directives.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. dialogHeaderEl.style.cssText += ';cursor:move;'
  9. dragDom.style.cssText += ';top:0px;'
  10. // 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
  11. const sty = (function () {
  12. //@ts-ignore
  13. if (window.document.currentStyle) {
  14. return (dom, attr) => dom.currentStyle[attr]
  15. } else {
  16. //@ts-ignore
  17. return (dom, attr) => getComputedStyle(dom, false)[attr]
  18. }
  19. })()
  20. dialogHeaderEl.onmousedown = (e) => {
  21. // 鼠标按下,计算当前元素距离可视区的距离
  22. const disX = e.clientX - dialogHeaderEl.offsetLeft
  23. const disY = e.clientY - dialogHeaderEl.offsetTop
  24. const screenWidth = document.body.clientWidth // body当前宽度
  25. const screenHeight = document.documentElement.clientHeight // 可见区域高度(应为body高度,可某些环境下无法获取)
  26. const dragDomWidth = dragDom.offsetWidth // 对话框宽度
  27. const dragDomheight = dragDom.offsetHeight // 对话框高度
  28. const minDragDomLeft = dragDom.offsetLeft
  29. const maxDragDomLeft = screenWidth - dragDom.offsetLeft - dragDomWidth
  30. const minDragDomTop = dragDom.offsetTop
  31. const maxDragDomTop = screenHeight - dragDom.offsetTop - dragDomheight
  32. // 获取到的值带px 正则匹配替换
  33. let styL = sty(dragDom, 'left')
  34. let styT = sty(dragDom, 'top')
  35. // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
  36. if (styL.includes('%')) {
  37. styL = +document.body.clientWidth * (+styL.replace(/\%/g, '') / 100)
  38. styT = +document.body.clientHeight * (+styT.replace(/\%/g, '') / 100)
  39. } else {
  40. styL = +styL.replace(/\px/g, '')
  41. styT = +styT.replace(/\px/g, '')
  42. }
  43. document.onmousemove = function (e) {
  44. // 通过事件委托,计算移动的距离
  45. let left = e.clientX - disX
  46. let top = e.clientY - disY
  47. // 边界处理
  48. if (-(left) > minDragDomLeft) {
  49. left = -(minDragDomLeft)
  50. } else if (left > maxDragDomLeft) {
  51. left = maxDragDomLeft
  52. }
  53. if (-(top) > minDragDomTop) {
  54. top = -(minDragDomTop)
  55. } else if (top > maxDragDomTop) {
  56. top = maxDragDomTop
  57. }
  58. // 移动当前元素
  59. dragDom.style.cssText += `;left:${left + styL}px;top:${top + styT}px;`
  60. }
  61. document.onmouseup = function (e) {
  62. document.onmousemove = null
  63. document.onmouseup = null
  64. }
  65. }
  66. }
  67. })
  68. // v-drag 框拖动
  69. Vue.directive('drag', {
  70. bind(el) {
  71. const oDiv = el // 当前元素
  72. const self = this // 上下文
  73. // 禁止选择网页上的文字
  74. document.onselectstart = function () {
  75. return false
  76. }
  77. oDiv.onmousedown = function (e) {
  78. // 鼠标按下,计算当前元素距离可视区的距离
  79. const disX = e.clientX - oDiv.offsetLeft
  80. const disY = e.clientY - oDiv.offsetTop
  81. document.onmousemove = function (e) {
  82. // 通过事件委托,计算移动的距离
  83. const l = e.clientX - disX
  84. const t = e.clientY - disY
  85. // 移动当前元素
  86. oDiv.style.left = l + 'px'
  87. oDiv.style.top = t + 'px'
  88. }
  89. document.onmouseup = function (e) {
  90. document.onmousemove = null
  91. document.onmouseup = null
  92. }
  93. // return false不加的话可能导致黏连,就是拖到一个地方时div粘在鼠标上不下来,相当于onmouseup失效
  94. // return false
  95. }
  96. }
  97. })
  98. //大屏浮动窗指令
  99. // v-Gpdrag 框拖动
  100. Vue.directive('Gpdrag', {
  101. bind(el) {
  102. const oDiv = el.querySelector('.header') // 当前元素
  103. const dDiv = el
  104. const self = this // 上下文
  105. // 禁止选择网页上的文字
  106. document.onselectstart = function () {
  107. return false
  108. }
  109. oDiv.onmousedown = function (e) {
  110. // 鼠标按下,计算当前元素距离可视区的距离
  111. const disX = e.clientX - dDiv.offsetLeft
  112. const disY = e.clientY - dDiv.offsetTop
  113. document.onmousemove = function (e) {
  114. // 通过事件委托,计算移动的距离
  115. const l = e.clientX - disX
  116. const t = e.clientY - disY
  117. // 移动当前元素
  118. dDiv.style.left = l + 'px'
  119. dDiv.style.top = t + 'px'
  120. }
  121. document.onmouseup = function (e) {
  122. document.onmousemove = null
  123. document.onmouseup = null
  124. }
  125. // return false不加的话可能导致黏连,就是拖到一个地方时div粘在鼠标上不下来,相当于onmouseup失效
  126. // return false
  127. }
  128. }
  129. })