yq1 1 year ago
parent
commit
d9d583d8a0
1 changed files with 52 additions and 0 deletions
  1. 52 0
      src/utils/move.js

+ 52 - 0
src/utils/move.js View File

1
+// 鼠标移动滚动位置类
2
+class Drag {
3
+  constructor(vm) {
4
+    this.dragWrap = vm;// 要挂载的容器
5
+    this._dom = {};
6
+    this._x = 0;
7
+    this._y = 0;
8
+    this._top = 0;
9
+    this._left = 0;
10
+    this.move = false;
11
+    this.down = false;
12
+    this.init.apply(this, arguments);
13
+  }
14
+
15
+  // 绑定事件
16
+  init() {
17
+    this.bindEvent();
18
+  }
19
+
20
+  // 给要素增加鼠标事件mousedown,mouseup,mousemove
21
+  bindEvent() {
22
+    var t = this;
23
+    this.dragWrap.addEventListener('mousedown', function (e) {
24
+      e && e.preventDefault();
25
+      if (!t.move) {
26
+        t.move = false;
27
+        t.down = true;
28
+        t._x = e.clientX;
29
+        t._y = e.clientY;
30
+        t._top = t.dragWrap.scrollTop;
31
+        t._left = t.dragWrap.scrollLeft;
32
+      }
33
+    });
34
+    this.dragWrap.addEventListener('mouseup', function (e) {
35
+      e && e.preventDefault();
36
+      t.move = false;
37
+      t.down = false;
38
+    });
39
+    this.dragWrap.addEventListener('mousemove', function (e) {
40
+      if (t.down) {
41
+        e && e.preventDefault();
42
+        t.move = true;
43
+        let x = t._x - e.clientX;
44
+        let y = t._y - e.clientY;
45
+        t.dragWrap.scrollLeft = t._left + x;
46
+        t.dragWrap.scrollTop = t._top + y;
47
+      }
48
+    });
49
+  }
50
+}
51
+export default Drag;
52
+