dialog.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. export default {
  2. bind(el, binding, vnode, oldVnode) {
  3. const resizeEvent = new CustomEvent('drag-resize', {
  4. detail: '尺寸变化',
  5. bubbles: false
  6. })
  7. // 初始化不最大化
  8. el.fullscreen = false
  9. // 弹框可拉伸最小宽高
  10. // const minWidth = 1100
  11. // const minHeight = 570
  12. const minWidth = 1920
  13. const minHeight = 1080
  14. // 当前宽高
  15. let nowWidth = minWidth
  16. // eslint-disable-next-line no-unused-vars
  17. let nowHight = minHeight
  18. // 当前顶部高度
  19. let nowMarginTop = 250
  20. // 获取弹框头部(这部分可双击全屏)
  21. const dialogHeaderEl = el.querySelector('.el-dialog__header')
  22. let hasSetBodyHight = false
  23. // 弹窗
  24. const dragDom = el.querySelector('.el-dialog')
  25. el.style.overflow = 'initial'
  26. dragDom.className += ' el-drag-dialog'
  27. // 给弹窗加上overflow auto;不然缩小时框内的标签可能超出dialog;
  28. dragDom.style.overflow = 'auto'
  29. // 清除选择头部文字效果
  30. // eslint-disable-next-line no-new-func
  31. dialogHeaderEl.onselectstart = new Function('return false')
  32. // 头部加上可拖动cursor
  33. dialogHeaderEl.style.cursor = 'move'
  34. // 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
  35. const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null)
  36. // 头部插入最大化最小化元素
  37. const maxMin = document.createElement('button')
  38. maxMin.className += ' el-dialog__headerbtn el-dialog__minmax'
  39. maxMin.style.right = '100px' // 全屏icon位置
  40. maxMin.style.display = 'none' // 全屏icon隐藏
  41. maxMin.style.color = 'white' // 全屏icon颜色
  42. maxMin.title = el.fullscreen ? '还原' : '最大化'
  43. maxMin.innerHTML = '<i class=' + (el.fullscreen ? '"el-icon-crop"' : '"el-icon-full-screen"') + ' onMouseOver="this.style.color=\'#409EFF\'" onMouseOut="this.style.color=\'inherit\'"></i>'
  44. dialogHeaderEl.insertBefore(maxMin, dialogHeaderEl.childNodes[1])
  45. const moveDown = (e) => {
  46. // 鼠标按下,计算当前元素距离可视区的距离
  47. const disX = e.clientX - dialogHeaderEl.offsetLeft
  48. const disY = e.clientY - dialogHeaderEl.offsetTop
  49. // 获取到的值带px 正则匹配替换
  50. let styL, styT
  51. // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
  52. if (sty.left.includes('%')) {
  53. styL = +document.body.clientWidth * (+sty.left.replace(/\\%/g, '') / 100)
  54. styT = +document.body.clientHeight * (+sty.top.replace(/\\%/g, '') / 100)
  55. } else {
  56. styL = +sty.left.replace(/\px/g, '')
  57. styT = +sty.top.replace(/\px/g, '')
  58. }
  59. document.onmousemove = function(e) {
  60. // 通过事件委托,计算移动的距离
  61. const l = e.clientX - disX
  62. const t = e.clientY - disY
  63. // 移动当前元素
  64. dragDom.style.left = `${l + styL}px`
  65. dragDom.style.top = `${t + styT}px`
  66. // 将此时的位置传出去
  67. // binding.value({x:e.pageX,y:e.pageY})
  68. }
  69. document.onmouseup = function(e) {
  70. document.onmousemove = null
  71. document.onmouseup = null
  72. }
  73. }
  74. dialogHeaderEl.onmousedown = moveDown
  75. let bodyHeight = 'auto'
  76. // function setMaxMin() {
  77. // if (el.fullscreen) {
  78. // const i = maxMin.querySelector('.el-icon-crop')
  79. // i.classList.remove('el-icon-crop')
  80. // i.classList.add('el-icon-full-screen')
  81. // maxMin.innerHTML = '<i class="el-icon-full-screen"></i>'
  82. // maxMin.title = '最大化'
  83. // dragDom.style.height = nowHight + 'px'
  84. // dragDom.style.width = nowWidth + 'px'
  85. // dragDom.style.marginTop = window.innerHeight * 0.07 + 'px'
  86. // el.fullscreen = false
  87. // dialogHeaderEl.style.cursor = 'move'
  88. // dialogHeaderEl.onmousedown = moveDown
  89. // // dragDom.querySelector('.el-dialog__body').style.height = bodyHeight;
  90. // hasSetBodyHight = false
  91. // } else {
  92. // const i = maxMin.querySelector('.el-icon-full-screen')
  93. // i.classList.remove('el-icon-full-screen')
  94. // i.classList.add('el-icon-crop')
  95. // maxMin.title = '还原'
  96. // bodyHeight = dragDom.querySelector('.el-dialog__body').offsetHeight + 'px'
  97. // nowHight = dragDom.clientHeight
  98. // nowWidth = dragDom.clientWidth
  99. // nowMarginTop = dragDom.style.marginTop
  100. // dragDom.style.left = 0
  101. // dragDom.style.top = 0
  102. // dragDom.style.height = window.innerHeight + 'px'
  103. // dragDom.style.width = '100VW'
  104. // dragDom.style.marginTop = 0
  105. // el.fullscreen = true
  106. // dialogHeaderEl.style.cursor = 'initial'
  107. // dialogHeaderEl.onmousedown = null
  108. // if (!hasSetBodyHight) {
  109. // const footerHeight = dragDom.querySelector('.el-dialog__footer') && dragDom.querySelector('.el-dialog__footer').offsetHeight
  110. // dragDom.querySelector('.el-dialog__body').style.height =
  111. // 'calc(90% - ' + (dialogHeaderEl.offsetHeight + footerHeight) + 'px)'
  112. // /* dragDom.querySelector('.el-dialog__body').style.height =
  113. // window.innerHeight*0.9
  114. // - (dialogHeaderEl.offsetHeight + footerHeight) + 'px'*/
  115. // hasSetBodyHight = true
  116. // }
  117. // }
  118. // el.dispatchEvent(resizeEvent)
  119. // }
  120. // 点击放大缩小效果
  121. // maxMin.onclick = setMaxMin
  122. // 双击头部效果
  123. // dialogHeaderEl.ondblclick = setMaxMin
  124. // 拉伸
  125. // const resizeEl = document.createElement('div')
  126. // dragDom.appendChild(resizeEl)
  127. // 在弹窗右下角加上一个10-10px的控制块
  128. // resizeEl.style.cursor = 'se-resize'
  129. // resizeEl.style.position = 'absolute'
  130. // resizeEl.style.height = '10px'
  131. // resizeEl.style.width = '10px'
  132. // resizeEl.style.right = '0px'
  133. // resizeEl.style.bottom = '0px'
  134. // resizeEl.style.zIndex = '99'
  135. // 鼠标拉伸弹窗
  136. // resizeEl.onmousedown = (e) => {
  137. // // 记录初始x位置
  138. // const clientX = e.clientX
  139. // // 鼠标按下,计算当前元素距离可视区的距离
  140. // const disX = e.clientX - resizeEl.offsetLeft
  141. // const disY = e.clientY - resizeEl.offsetTop
  142. // document.onmousemove = function(e) {
  143. // e.preventDefault() // 移动时禁用默认事件
  144. // // 通过事件委托,计算移动的距离
  145. // const x = e.clientX - disX + (e.clientX - clientX)// 这里 由于elementUI的dialog控制居中的,所以水平拉伸效果是双倍
  146. // const y = e.clientY - disY
  147. // // 比较是否小于最小宽高
  148. // dragDom.style.width = x > minWidth ? `${x}px` : minWidth + 'px'
  149. // dragDom.style.height = y > minHeight ? `${y}px` : minHeight + 'px'
  150. // if (!hasSetBodyHight) {
  151. // const footerHeight = dragDom.querySelector('.el-dialog__footer') && dragDom.querySelector('.el-dialog__footer').offsetHeight
  152. // dragDom.querySelector('.el-dialog__body').style.height = 'calc(90% - ' + (dialogHeaderEl.offsetHeight + footerHeight) + 'px)'
  153. // hasSetBodyHight = true
  154. // }
  155. // }
  156. // // 拉伸结束
  157. // document.onmouseup = function(e) {
  158. // document.onmousemove = null
  159. // document.onmouseup = null
  160. // el.dispatchEvent(resizeEvent)
  161. // }
  162. // }
  163. }
  164. }