dragDesignPanel.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * @description: dashboard画布拖拽
  3. * @Date: 2023-03-20 12:48:42
  4. * @Author: xing.heng
  5. */
  6. const dragDesignPanelMixin = {
  7. data () {
  8. return {
  9. isMouseDown: false,
  10. isContextmenu: false,
  11. scrollTop: 0,
  12. scrollLeft: 0,
  13. _startX: 0,
  14. _startY: 0
  15. }
  16. },
  17. mounted () {
  18. this.moveCanvas()
  19. },
  20. methods: {
  21. moveCanvas () {
  22. this.$nextTick(() => {
  23. const father = document.querySelector('#screens')
  24. father.addEventListener('contextmenu', e => {
  25. e.preventDefault()
  26. e.stopPropagation()
  27. this.isContextmenu = true
  28. })
  29. father.addEventListener('mousedown', e => {
  30. e.preventDefault()
  31. e.stopPropagation()
  32. if (e.button === 2) {
  33. this.isMouseDown = true
  34. // 记录按下鼠标的位置, 用于计算鼠标的移动距离
  35. this._startX = e.offsetX
  36. this._startY = e.offsetY
  37. }
  38. })
  39. father.addEventListener('mousemove', this.handleMove)
  40. father.addEventListener('mouseup', (e) => {
  41. e.preventDefault()
  42. e.stopPropagation()
  43. this.resetIsMouseDown()
  44. // father.removeEventListener('mousemove', this.handleMove)
  45. })
  46. father.addEventListener('mouseleave', (e) => {
  47. e.preventDefault()
  48. e.stopPropagation()
  49. this.resetIsMouseDown()
  50. })
  51. })
  52. },
  53. handleMove (e) {
  54. const father = document.querySelector('#screens')
  55. e.preventDefault()
  56. e.stopPropagation()
  57. // 判断鼠标移动时是否处于按下状态
  58. if (this.isMouseDown && e.buttons === 2) {
  59. // 获取鼠标按下后移动的距离
  60. const offsetX = e.offsetX - this._startX
  61. const offsetY = e.offsetY - this._startY
  62. // 需要注意的是当鼠标向上移动时, 滚动条应该向下移动, 所以这里都是减去的移动距离
  63. this.scrollTop = this.scrollTop - offsetY
  64. this.scrollLeft = this.scrollLeft - offsetX
  65. // 横向可移动的最大距离
  66. const limitX = father.scrollWidth - father.offsetWidth
  67. // 纵向可移动的最大距离
  68. const limitY = father.scrollHeight - father.offsetHeight
  69. if (this.scrollTop >= limitY) {
  70. // 当滑块移动到底端时
  71. this.scrollTop = limitY
  72. } else if (this.scrollTop <= 0) {
  73. // 当滑块移动到顶端时
  74. this.scrollTop = 0
  75. }
  76. if (this.scrollLeft >= limitX) {
  77. // 当滑块移动到左侧时
  78. this.scrollLeft = limitX
  79. } else if (this.scrollLeft <= 0) {
  80. // 当滑块移动到右侧时
  81. this.scrollLeft = 0
  82. }
  83. // 标尺开始的刻度
  84. const startX = (father.scrollLeft + this.thick) - offsetX
  85. const startY = (father.scrollTop + this.thick) - offsetY
  86. if (
  87. this.startX >= 0 &&
  88. this.startY >= 0 &&
  89. this.startX <= (this.width - father.scrollLeft) &&
  90. this.startY <= (this.height - father.scrollTop)
  91. ) {
  92. this.startX = startX
  93. this.startY = startY
  94. }
  95. // 将计算后的距离赋值给滚动条
  96. father.scrollTop = this.scrollTop
  97. father.scrollLeft = this.scrollLeft
  98. // this.$emit('changeStart', {
  99. // x: this.startX + 50 - 20,
  100. // y: this.startY + 50 - 20
  101. // })
  102. }
  103. },
  104. resetIsMouseDown () {
  105. this.isMouseDown = false
  106. this.isContextmenu = false
  107. }
  108. }
  109. }
  110. export { dragDesignPanelMixin }