-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathSwipe.js
More file actions
118 lines (94 loc) · 3.51 KB
/
Copy pathSwipe.js
File metadata and controls
118 lines (94 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* @namespace BMapGL的所有library类均放在BMapGLLib命名空间下
*/
let BMapLib = window.BMapLib || {};
const prefix = 'BMapLib';
class Swipe {
constructor(a, b, container, options = {}) {
this._container = container;
this._mapA = a;
this._mapB = b;
// swipe中线位置
this._centerX = 0;
this._mapA.disableTilt();
this._mapB.disableTilt();
this._mapA.disableRotate();
this._mapB.disableRotate();
this._initDom();
this._syncMapA = this._syncMapA.bind(this);
this._syncMapB = this._syncMapB.bind(this);
this._onSync();
}
_initDom() {
this._swipeControlDiv = document.createElement('div');
this._swipeControlDiv.className = `${prefix}-swipe`;
this._swipeBtn = document.createElement('div');
this._swipeBtn.className = `${prefix}-swipe-btn`;
this._swipeControlDiv.appendChild(this._swipeBtn);
this._onSwipeStart = this._onSwipeStart.bind(this);
this._onSwipeMove = this._onSwipeMove.bind(this);
this._onSwipeEnd = this._onSwipeEnd.bind(this);
this._swipeBtn.addEventListener('mousedown', this._onSwipeStart);
this._container.appendChild(this._swipeControlDiv);
this._centerX = parseInt(getComputedStyle(this._swipeControlDiv).left);
this._containerWidth = parseInt(getComputedStyle(this._container).width);
const halfWidth = this._containerWidth / 2;
this._updateSwipe(halfWidth);
}
_onSwipeStart() {
document.addEventListener('mousemove', this._onSwipeMove);
document.addEventListener('mouseup', this._onSwipeEnd);
}
_onSwipeMove(e) {
let x = e.clientX - this._centerX;
this._swipeControlDiv.style.transform = `translateX(${x}px)`;
this._updateSwipe(e.clientX);
}
_onSwipeEnd() {
document.removeEventListener('mousemove', this._onSwipeMove);
document.removeEventListener('mouseup', this._onSwipeEnd);
}
_updateSwipe(x) {
const clipA = `polygon(0 0, ${x}px 0, ${x}px 100%, 0 100%)`;
const clipB = `polygon(${x}px 0, 100% 0, 100% 100%, ${x}px 100%)`;
this._mapA.getContainer().style.clipPath = clipA;
this._mapB.getContainer().style.clipPath = clipB;
}
_onSync() {
this._mapA.addEventListener('moving', this._syncMapA);
this._mapA.addEventListener('zooming', this._syncMapA);
this._mapB.addEventListener('moving', this._syncMapB);
this._mapB.addEventListener('zooming', this._syncMapB);
}
_offSync() {
this._mapA.removeEventListener('moving', this._syncMapA);
this._mapA.removeEventListener('zooming', this._syncMapA);
this._mapB.removeEventListener('moving', this._syncMapB);
this._mapB.removeEventListener('zooming', this._syncMapB);
}
_syncMapA() {
this._offSync();
const zoom = this._mapA.getZoom();
const center = this._mapA.getCenter();
const that = this;
this._mapB.centerAndZoom(center, zoom, {
noAnimation: true,
callback() {
that._onSync();
},
});
}
_syncMapB() {
this._offSync();
const zoom = this._mapB.getZoom();
const center = this._mapB.getCenter();
const that = this;
this._mapA.centerAndZoom(center, zoom, {
noAnimation: true,
callback() {
that._onSync();
},
});
}
}
BMapLib.Swipe = Swipe;