-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
234 lines (229 loc) · 8.31 KB
/
index.html
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>拖拽</title>
</head>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.grid {
display: flex;
flex-wrap: wrap;
margin: 0 -15px -15px 0;
touch-action: none;
user-select: none;
}
.grid-item {
width: 90px;
height: 90px;
line-height: 88px;
text-align: center;
margin: 0 15px 15px 0;
background: #fff;
border: 1px solid #d6d6d6;
list-style: none;
}
.active {
background: #c8ebfb;
}
.clone-grid-item {
position: fixed;
left: 0;
top: 0;
z-index: 1;
width: 90px;
height: 90px;
line-height: 88px;
text-align: center;
background: #fff;
border: 1px solid #d6d6d6;
opacity: 0.8;
list-style: none;
}
</style>
<body>
<ul class="grid">
<li class="grid-item">item1</li>
<li class="grid-item">item2</li>
<li class="grid-item">item3</li>
<li class="grid-item">item4</li>
<li class="grid-item">item5</li>
<li class="grid-item">item6</li>
<li class="grid-item">item7</li>
<li class="grid-item">item8</li>
<li class="grid-item">item9</li>
<li class="grid-item">item10</li>
</ul>
</body>
<script>
class Draggable {
constructor(options) {
this.parent = options.element; // 父级元素
this.cloneElementClassName = options.cloneElementClassName; // 克隆元素类名
this.isPointerdown = false;
this.diff = { x: 0, y: 0 }; // 相对于上一次移动差值
this.drag = { element: null, index: 0, lastIndex: 0 }; // 拖拽元素
this.drop = { element: null, index: 0, lastIndex: 0 }; // 释放元素
this.clone = { element: null, x: 0, y: 0 };
this.lastPointermove = { x: 0, y: 0 };
this.rectList = []; // 用于保存拖拽项getBoundingClientRect()方法获得的数据
this.init();
}
init() {
this.getRect();
this.bindEventListener();
}
// 获取元素位置信息
getRect() {
this.rectList.length = 0;
for (const item of this.parent.children) {
this.rectList.push(item.getBoundingClientRect());
}
}
handlePointerdown(e) {
// 如果是鼠标点击,只响应左键
if (e.pointerType === 'mouse' && e.button !== 0) {
return;
}
if (e.target === this.parent) {
return;
}
this.isPointerdown = true;
this.parent.setPointerCapture(e.pointerId);
this.lastPointermove.x = e.clientX;
this.lastPointermove.y = e.clientY;
this.drag.element = e.target;
this.drag.element.classList.add('active');
this.clone.element = this.drag.element.cloneNode(true);
this.clone.element.className = this.cloneElementClassName;
this.clone.element.style.transition = 'none';
const i = [].indexOf.call(this.parent.children, this.drag.element);
this.clone.x = this.rectList[i].left;
this.clone.y = this.rectList[i].top;
this.drag.index = i;
this.drag.lastIndex = i;
this.clone.element.style.transform =
'translate3d(' + this.clone.x + 'px, ' + this.clone.y + 'px, 0)';
document.body.appendChild(this.clone.element);
}
handlePointermove(e) {
if (this.isPointerdown) {
this.diff.x = e.clientX - this.lastPointermove.x;
this.diff.y = e.clientY - this.lastPointermove.y;
this.lastPointermove.x = e.clientX;
this.lastPointermove.y = e.clientY;
this.clone.x += this.diff.x;
this.clone.y += this.diff.y;
this.clone.element.style.transform =
'translate3d(' + this.clone.x + 'px, ' + this.clone.y + 'px, 0)';
for (let i = 0; i < this.rectList.length; i++) {
// 碰撞检测
if (
e.clientX > this.rectList[i].left &&
e.clientX < this.rectList[i].right &&
e.clientY > this.rectList[i].top &&
e.clientY < this.rectList[i].bottom
) {
this.drop.element = this.parent.children[i];
this.drop.lastIndex = i;
if (this.drag.element !== this.drop.element) {
if (this.drag.index < i) {
this.parent.insertBefore(
this.drag.element,
this.drop.element.nextElementSibling
);
this.drop.index = i - 1;
} else {
this.parent.insertBefore(
this.drag.element,
this.drop.element
);
this.drop.index = i + 1;
}
this.drag.index = i;
const dragRect = this.rectList[this.drag.index];
const lastDragRect = this.rectList[this.drag.lastIndex];
const dropRect = this.rectList[this.drop.index];
const lastDropRect = this.rectList[this.drop.lastIndex];
this.drag.lastIndex = i;
this.drag.element.style.transition = 'none';
this.drop.element.style.transition = 'none';
this.drag.element.style.transform =
'translate3d(' +
(lastDragRect.left - dragRect.left) +
'px, ' +
(lastDragRect.top - dragRect.top) +
'px, 0)';
this.drop.element.style.transform =
'translate3d(' +
(lastDropRect.left - dropRect.left) +
'px, ' +
(lastDropRect.top - dropRect.top) +
'px, 0)';
this.drag.element.offsetLeft; // 触发重绘
this.drag.element.style.transition = 'transform 150ms';
this.drop.element.style.transition = 'transform 150ms';
this.drag.element.style.transform =
'translate3d(0px, 0px, 0px)';
this.drop.element.style.transform =
'translate3d(0px, 0px, 0px)';
}
break;
}
}
}
}
handlePointerup(e) {
if (this.isPointerdown) {
this.isPointerdown = false;
this.drag.element.classList.remove('active');
this.clone.element.remove();
}
}
handlePointercancel(e) {
if (this.isPointerdown) {
this.isPointerdown = false;
this.drag.element.classList.remove('active');
this.clone.element.remove();
}
}
bindEventListener() {
this.handlePointerdown = this.handlePointerdown.bind(this);
this.handlePointermove = this.handlePointermove.bind(this);
this.handlePointerup = this.handlePointerup.bind(this);
this.handlePointercancel = this.handlePointercancel.bind(this);
this.getRect = this.getRect.bind(this);
this.parent.addEventListener('pointerdown', this.handlePointerdown);
this.parent.addEventListener('pointermove', this.handlePointermove);
this.parent.addEventListener('pointerup', this.handlePointerup);
this.parent.addEventListener('pointercancel', this.handlePointercancel);
window.addEventListener('scroll', this.getRect);
window.addEventListener('resize', this.getRect);
window.addEventListener('orientationchange', this.getRect);
}
unbindEventListener() {
this.parent.removeEventListener('pointerdown', this.handlePointerdown);
this.parent.removeEventListener('pointermove', this.handlePointermove);
this.parent.removeEventListener('pointerup', this.handlePointerup);
this.parent.removeEventListener(
'pointercancel',
this.handlePointercancel
);
window.removeEventListener('scroll', this.getRect);
window.removeEventListener('resize', this.getRect);
window.removeEventListener('orientationchange', this.getRect);
}
}
// 实例化
new Draggable({
element: document.querySelector('.grid'),
cloneElementClassName: 'clone-grid-item',
});
</script>
</html>