DragBox.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <script>
  2. export default {
  3. data() {
  4. return {
  5. dragBox: {
  6. top: 0,
  7. left: 0,
  8. pos1: 0,
  9. pos2: 0,
  10. pos3: 0,
  11. pos4: 0,
  12. width: 400,
  13. height: 50,
  14. initial: {
  15. top: 0,
  16. left: 0
  17. },
  18. latest: {
  19. top: null,
  20. left: null
  21. },
  22. debounceTimeout: null,
  23. lastTappedDate: 0
  24. }
  25. };
  26. },
  27. mounted() {
  28. this.resetBoxPosition(true);
  29. this.$nextTick(() => {
  30. this.onWindowResizeDragBox();
  31. window.addEventListener("resize", this.onWindowResizeDragBox);
  32. });
  33. },
  34. unmounted() {
  35. window.removeEventListener("resize", this.onWindowResizeDragBox);
  36. if (this.dragBox.debounceTimeout)
  37. clearTimeout(this.dragBox.debounceTimeout);
  38. },
  39. methods: {
  40. setInitialBox(initial, reset) {
  41. this.dragBox.initial = initial || this.dragBox.initial;
  42. if (reset)
  43. this.dragBox = { ...this.dragBox, ...this.dragBox.initial };
  44. },
  45. onDragBox(e) {
  46. const e1 = e || window.event;
  47. const e1IsTouch = e1.type === "touchstart";
  48. e1.preventDefault();
  49. if (e1IsTouch) {
  50. // Handle double click from touch (if this method is twice in a row within one second)
  51. if (Date.now() - this.dragBox.lastTappedDate <= 1000) {
  52. this.resetBoxPosition();
  53. this.dragBox.lastTappedDate = 0;
  54. return;
  55. }
  56. this.dragBox.lastTappedDate = Date.now();
  57. }
  58. this.dragBox.pos3 = e1IsTouch
  59. ? e1.changedTouches[0].clientX
  60. : e1.clientX;
  61. this.dragBox.pos4 = e1IsTouch
  62. ? e1.changedTouches[0].clientY
  63. : e1.clientY;
  64. document.onmousemove = document.ontouchmove = e => {
  65. const e2 = e || window.event;
  66. const e2IsTouch = e2.type === "touchmove";
  67. if (!e2IsTouch) e2.preventDefault();
  68. // Get the clientX and clientY
  69. const e2ClientX = e2IsTouch
  70. ? e2.changedTouches[0].clientX
  71. : e2.clientX;
  72. const e2ClientY = e2IsTouch
  73. ? e2.changedTouches[0].clientY
  74. : e2.clientY;
  75. // calculate the new cursor position:
  76. this.dragBox.pos1 = this.dragBox.pos3 - e2ClientX;
  77. this.dragBox.pos2 = this.dragBox.pos4 - e2ClientY;
  78. this.dragBox.pos3 = e2ClientX;
  79. this.dragBox.pos4 = e2ClientY;
  80. // set the element's new position:
  81. this.dragBox.top -= this.dragBox.pos2;
  82. this.dragBox.left -= this.dragBox.pos1;
  83. if (
  84. this.dragBox.top >
  85. document.body.clientHeight - this.dragBox.height
  86. )
  87. this.dragBox.top =
  88. document.body.clientHeight - this.dragBox.height;
  89. if (this.dragBox.top < 0) this.dragBox.top = 0;
  90. if (
  91. this.dragBox.left >
  92. document.body.clientWidth - this.dragBox.width
  93. )
  94. this.dragBox.left =
  95. document.body.clientWidth - this.dragBox.width;
  96. if (this.dragBox.left < 0) this.dragBox.left = 0;
  97. };
  98. document.onmouseup = document.ontouchend = () => {
  99. document.onmouseup = null;
  100. document.ontouchend = null;
  101. document.onmousemove = null;
  102. document.ontouchmove = null;
  103. if (typeof this.onDragBoxUpdate === "function")
  104. this.onDragBoxUpdate();
  105. };
  106. },
  107. resetBoxPosition(preventUpdate) {
  108. this.setInitialBox(null, true);
  109. this.dragBox.latest.top = this.dragBox.top;
  110. this.dragBox.latest.left = this.dragBox.left;
  111. if (!preventUpdate && typeof this.onDragBoxUpdate === "function")
  112. this.onDragBoxUpdate();
  113. },
  114. onWindowResizeDragBox() {
  115. if (this.dragBox.debounceTimeout)
  116. clearTimeout(this.dragBox.debounceTimeout);
  117. this.dragBox.debounceTimeout = setTimeout(() => {
  118. if (
  119. this.dragBox.top === this.dragBox.latest.top &&
  120. this.dragBox.left === this.dragBox.latest.left
  121. )
  122. this.resetBoxPosition();
  123. else {
  124. if (
  125. this.dragBox.top >
  126. document.body.clientHeight - this.dragBox.height
  127. )
  128. this.dragBox.top =
  129. document.body.clientHeight - this.dragBox.height;
  130. if (this.dragBox.top < 0) this.dragBox.top = 0;
  131. if (
  132. this.dragBox.left >
  133. document.body.clientWidth - this.dragBox.width
  134. )
  135. this.dragBox.left =
  136. document.body.clientWidth - this.dragBox.width;
  137. if (this.dragBox.left < 0) this.dragBox.left = 0;
  138. if (typeof this.onDragBoxUpdate === "function")
  139. this.onDragBoxUpdate();
  140. }
  141. }, 50);
  142. }
  143. }
  144. };
  145. </script>