customDirective.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import type { App } from 'vue'
  2. /**
  3. * 按钮波浪指令
  4. * @directive 默认方式:v-waves,如 `<div v-waves></div>`
  5. * @directive 参数方式:v-waves=" |light|red|orange|purple|green|teal",如 `<div v-waves="'light'"></div>`
  6. */
  7. export function wavesDirective(app: App) {
  8. app.directive('waves', {
  9. mounted(el, binding) {
  10. el.classList.add('waves-effect')
  11. binding.value && el.classList.add(`waves-${binding.value}`)
  12. function setConvertStyle(obj: { [key: string]: unknown }) {
  13. let style: string = ''
  14. for (let i in obj) {
  15. if (obj.hasOwnProperty(i)) style += `${i}:${obj[i]};`
  16. }
  17. return style
  18. }
  19. function onCurrentClick(e: { [key: string]: unknown }) {
  20. let elDiv = document.createElement('div')
  21. elDiv.classList.add('waves-ripple')
  22. el.appendChild(elDiv)
  23. let styles = {
  24. left: `${e.layerX}px`,
  25. top: `${e.layerY}px`,
  26. opacity: 1,
  27. transform: `scale(${(el.clientWidth / 100) * 10})`,
  28. 'transition-duration': `750ms`,
  29. 'transition-timing-function': `cubic-bezier(0.250, 0.460, 0.450, 0.940)`,
  30. }
  31. elDiv.setAttribute('style', setConvertStyle(styles))
  32. setTimeout(() => {
  33. elDiv.setAttribute(
  34. 'style',
  35. setConvertStyle({
  36. opacity: 0,
  37. transform: styles.transform,
  38. left: styles.left,
  39. top: styles.top,
  40. })
  41. )
  42. setTimeout(() => {
  43. elDiv && el.removeChild(elDiv)
  44. }, 750)
  45. }, 450)
  46. }
  47. el.addEventListener('mousedown', onCurrentClick, false)
  48. },
  49. unmounted(el) {
  50. el.addEventListener('mousedown', () => {})
  51. },
  52. })
  53. }
  54. /**
  55. * 自定义拖动指令
  56. * @description 使用方式:v-drag="[dragDom,dragHeader]",如 `<div v-drag="['.drag-container .el-dialog', '.drag-container .el-dialog__header']"></div>`
  57. * @description dragDom 要拖动的元素,dragHeader 要拖动的 Header 位置
  58. * @link 注意:https://github.com/element-plus/element-plus/issues/522
  59. * @lick 参考:https://blog.csdn.net/weixin_46391323/article/details/105228020?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-10&spm=1001.2101.3001.4242
  60. */
  61. export function dragDirective(app: App) {
  62. app.directive('drag', {
  63. mounted(el, binding) {
  64. if (!binding.value) return false
  65. const dragDom = document.querySelector(binding.value[0]) as HTMLElement
  66. const dragHeader = document.querySelector(binding.value[1]) as HTMLElement
  67. dragHeader.onmouseover = () => (dragHeader.style.cursor = `move`)
  68. function down(e: any, type: string) {
  69. // 鼠标按下,计算当前元素距离可视区的距离
  70. const disX = type === 'pc' ? e.clientX - dragHeader.offsetLeft : e.touches[0].clientX - dragHeader.offsetLeft
  71. const disY = type === 'pc' ? e.clientY - dragHeader.offsetTop : e.touches[0].clientY - dragHeader.offsetTop
  72. // body当前宽度
  73. const screenWidth = document.body.clientWidth
  74. // 可见区域高度(应为body高度,可某些环境下无法获取)
  75. const screenHeight = document.documentElement.clientHeight
  76. // 对话框宽度
  77. const dragDomWidth = dragDom.offsetWidth
  78. // 对话框高度
  79. const dragDomheight = dragDom.offsetHeight
  80. const minDragDomLeft = dragDom.offsetLeft
  81. const maxDragDomLeft = screenWidth - dragDom.offsetLeft - dragDomWidth
  82. const minDragDomTop = dragDom.offsetTop
  83. const maxDragDomTop = screenHeight - dragDom.offsetTop - dragDomheight
  84. // 获取到的值带px 正则匹配替换
  85. let styL: any = getComputedStyle(dragDom).left
  86. let styT: any = getComputedStyle(dragDom).top
  87. // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
  88. if (styL.includes('%')) {
  89. styL = +document.body.clientWidth * (+styL.replace(/\%/g, '') / 100)
  90. styT = +document.body.clientHeight * (+styT.replace(/\%/g, '') / 100)
  91. } else {
  92. styL = +styL.replace(/\px/g, '')
  93. styT = +styT.replace(/\px/g, '')
  94. }
  95. return {
  96. disX,
  97. disY,
  98. minDragDomLeft,
  99. maxDragDomLeft,
  100. minDragDomTop,
  101. maxDragDomTop,
  102. styL,
  103. styT,
  104. }
  105. }
  106. function move(e: any, type: string, obj: any) {
  107. let { disX, disY, minDragDomLeft, maxDragDomLeft, minDragDomTop, maxDragDomTop, styL, styT } = obj
  108. // 通过事件委托,计算移动的距离
  109. let left = type === 'pc' ? e.clientX - disX : e.touches[0].clientX - disX
  110. let top = type === 'pc' ? e.clientY - disY : e.touches[0].clientY - disY
  111. // 边界处理
  112. if (-left > minDragDomLeft) {
  113. left = -minDragDomLeft
  114. } else if (left > maxDragDomLeft) {
  115. left = maxDragDomLeft
  116. }
  117. if (-top > minDragDomTop) {
  118. top = -minDragDomTop
  119. } else if (top > maxDragDomTop) {
  120. top = maxDragDomTop
  121. }
  122. // 移动当前元素
  123. dragDom.style.cssText += `;left:${left + styL}px;top:${top + styT}px;`
  124. }
  125. /**
  126. * pc端
  127. * onmousedown 鼠标按下触发事件
  128. * onmousemove 鼠标按下时持续触发事件
  129. * onmouseup 鼠标抬起触发事件
  130. */
  131. dragHeader.onmousedown = (e) => {
  132. const obj = down(e, 'pc')
  133. document.onmousemove = (e) => {
  134. move(e, 'pc', obj)
  135. }
  136. document.onmouseup = () => {
  137. document.onmousemove = null
  138. document.onmouseup = null
  139. }
  140. }
  141. /**
  142. * 移动端
  143. * ontouchstart 当按下手指时,触发ontouchstart
  144. * ontouchmove 当移动手指时,触发ontouchmove
  145. * ontouchend 当移走手指时,触发ontouchend
  146. */
  147. dragHeader.ontouchstart = (e) => {
  148. const obj = down(e, 'app')
  149. document.ontouchmove = (e) => {
  150. move(e, 'app', obj)
  151. }
  152. document.ontouchend = () => {
  153. document.ontouchmove = null
  154. document.ontouchend = null
  155. }
  156. }
  157. },
  158. })
  159. }