前言

Vue2实现一个包含大于2个手柄的滑块(以垂直方向为例)

image

思路

基本思路

  1. 初始化一个色条和一个同等高度的拖拽区
  2. 遍历数组,计算每个数组的位置,赋值给style.top/bottom
  3. 给拖拽元素绑定拖拽函数,拖拽时计算和赋值位置,根据位置计算变化后的值
  4. 触发事件回调

优化

  1. 防抖:触发事件回调可以外包一层防抖,防抖时延可以作为组件参数传入
  2. 结果排序:sort
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<!-- 
组件:多值滑条
start-起始值 [基础参数]
end-终止值 [基础参数]
heightArr-数组(Number-长度不限) [基础参数]
beyond-允许超出相邻边界,默认true [基础参数]
unit-单位,默认m
sortable-是否从小到大排序,默认排序
width-色条宽度,默认10px 至少给该组件所在容器一个60px的宽度
-->
<template>
<div class="temp">
<div class="height-container" :style="{ width: `${2 * width}px` }">
<div class="control" ref="controlRef">
<div class="end">{{ end }}{{ unit }}</div>
<div class="start">{{ start }}{{ unit }}</div>
<div
class="drag-item"
v-for="(item, index) in heightTemp"
:key="index"
@mousedown="startDrag(index, $event)"
>
{{ item }}{{ unit }}
</div>
</div>
<div class="slider"></div>
</div>
</div>
</template>
<script>
export default {
name: "MultiSlider",
props: {
width: {
type: Number,
default: 10,
},
start: {
type: Number,
default: 0,
},
end: {
type: Number,
default: 1000,
},
heightArr: {
type: Array,
default: () => [100, 300, 700],
},
sortable: {
type: Boolean,
default: true,
},
beyond: {
type: Boolean,
default: true,
},
unit: {
type: String,
default: "m",
},
},
data() {
return {
heightTemp: [], // 三个高度
dragging: false, // 拖拽中
dragIndex: null, // 拖拽的索引
dragTimer: null, // 拖拽定时器
precision: 0,
};
},
mounted() {
this.heightTemp = this.heightArr;
this.precision = this.heightTemp.reduce((max, cur) => {
let temp = cur.toString().split(".");
if (temp.length > 1) {
max = Math.max(max, temp[1].length || 0);
} else {
max = Math.max(max, 0);
}
return max;
}, 0);

this.$nextTick(() => {
this.initHeight();
});
},
methods: {
// 初始位置
initHeight() {
let start = this.start;
let end = this.end;
let sliderHeight = this.$refs.controlRef.clientHeight;
let dragItems = this.$refs.controlRef.querySelectorAll(".drag-item");
this.heightTemp.forEach((item, idx) => {
let bottom = ((item - start) / (end - start)) * sliderHeight;
dragItems[idx].style.bottom = `${bottom}px`;
});
},
// 点击开始拖拽
startDrag(index, event) {
event.preventDefault();
event.stopPropagation();
this.dragging = true;
this.dragIndex = index;
document.addEventListener("mousemove", this.drag);
document.addEventListener("mouseup", this.stopDrag);
},
// 拖拽中
drag(event) {
if (!this.dragging) return;
document.body.style.cursor = "grabbing";
let sliderHeight = this.$refs.controlRef.clientHeight;
let bottom =
sliderHeight -
(event.clientY - this.$refs.controlRef.getBoundingClientRect().top);
if (bottom < 0) {
bottom = 0;
}
if (bottom > sliderHeight) {
bottom = sliderHeight;
}

// 不允许超出上下边界
if (!this.beyond) {
let dragItems = this.$refs.controlRef.querySelectorAll(".drag-item");
// 获取bottom数值
let curBottom = dragItems[this.dragIndex].style.bottom;
// 转化为数字
curBottom = Number(curBottom.replace("px", ""));
if (this.dragIndex === 0) {
let prevBottom = dragItems[this.dragIndex + 1]?.style.bottom;
prevBottom = Number(prevBottom.replace("px", ""));
if (curBottom >= prevBottom - 12) {
bottom = prevBottom - 14;
this.$refs.controlRef.querySelectorAll(".drag-item")[
this.dragIndex
].style.bottom = `${bottom}px`;
let start = this.start;
let end = this.end;

let temp = (
(bottom / sliderHeight) * (end - start) +
start
).toFixed(this.precision);
this.heightTemp[this.dragIndex] = Number(temp);

this.$refs.controlRef.querySelectorAll(".drag-item")[
this.dragIndex
].innerText = `${temp}m`;
this.stopDrag();
}
} else if (this.dragIndex === dragItems.length - 1) {
let nextBottom =
dragItems[this.dragIndex - 1]?.style.bottom || sliderHeight;
nextBottom = Number(nextBottom.replace("px", ""));
if (curBottom <= nextBottom + 12) {
bottom = nextBottom + 14;
this.$refs.controlRef.querySelectorAll(".drag-item")[
this.dragIndex
].style.bottom = `${bottom}px`;
let start = this.start;
let end = this.end;

let temp = (
(bottom / sliderHeight) * (end - start) +
start
).toFixed(this.precision);
this.heightTemp[this.dragIndex] = Number(temp);

this.$refs.controlRef.querySelectorAll(".drag-item")[
this.dragIndex
].innerText = `${temp}m`;
this.stopDrag();
}
} else {
let prevBottom = dragItems[this.dragIndex + 1]?.style.bottom;
let nextBottom =
dragItems[this.dragIndex - 1]?.style.bottom || sliderHeight;
prevBottom = Number(prevBottom.replace("px", ""));
nextBottom = Number(nextBottom.replace("px", ""));
if (curBottom >= prevBottom - 12) {
bottom = prevBottom - 14;
this.$refs.controlRef.querySelectorAll(".drag-item")[
this.dragIndex
].style.bottom = `${bottom}px`;
let start = this.start;
let end = this.end;

let temp = (
(bottom / sliderHeight) * (end - start) +
start
).toFixed(this.precision);
this.heightTemp[this.dragIndex] = Number(temp);

this.$refs.controlRef.querySelectorAll(".drag-item")[
this.dragIndex
].innerText = `${temp}m`;
this.stopDrag();
} else if (curBottom <= nextBottom + 12) {
bottom = nextBottom + 14;
this.$refs.controlRef.querySelectorAll(".drag-item")[
this.dragIndex
].style.bottom = `${bottom}px`;
let start = this.start;
let end = this.end;

let temp = (
(bottom / sliderHeight) * (end - start) +
start
).toFixed(this.precision);
this.heightTemp[this.dragIndex] = Number(temp);

this.$refs.controlRef.querySelectorAll(".drag-item")[
this.dragIndex
].innerText = `${temp}m`;
this.stopDrag();
}
}
}
this.$refs.controlRef.querySelectorAll(".drag-item")[
this.dragIndex
].style.bottom = `${bottom}px`;
let start = this.start;
let end = this.end;

let temp = ((bottom / sliderHeight) * (end - start) + start).toFixed(
this.precision
);
this.heightTemp[this.dragIndex] = Number(temp);

this.$refs.controlRef.querySelectorAll(".drag-item")[
this.dragIndex
].innerText = `${temp}m`;

// this.setDebounce();
},
// 结束拖拽
stopDrag() {
this.dragging = false;
this.dragIndex = null;
document.body.style.cursor = "auto";
document.removeEventListener("mousemove", this.drag);
document.removeEventListener("mouseup", this.stopDrag);
this.setDebounce();
},
// 回调
setDebounce() {
if (this.sortable) {
let result = this.heightTemp.sort((a, b) => a - b);
this.$emit("changeSlider", result);
} else {
this.$emit("changeSlider", this.heightTemp);
}
},
},
};
</script>
<style lang="scss" scoped>
.temp {
width: 100%;
height: 100%;
display: flex;
justify-content: flex-end;
align-items: center;
}
.height-container {
height: 100%;
width: 100%;
font-size: 12px;
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr;
gap: 4px;
padding-top: 12px;

.control {
grid-column: span 1;
position: relative;

.start {
position: absolute;
bottom: 0;
right: 0;
color: #a09fa4;
user-select: none;
}
.end {
position: absolute;
top: -12px;
right: 0;
color: #a09fa4;
user-select: none;
}

.drag-item {
position: absolute;
right: 0;
width: fit-content;
user-select: none;
cursor: grab;
height: 16px;
padding-right: 4px;
padding-left: 2px;
color: #a09fa4;
background-color: #a09fa4;
clip-path: polygon(0 0, 88% 0, 100% 50%, 88% 100%, 0 100%);
display: flex;
align-items: center;
justify-content: center;
z-index: 10;
&::before {
z-index: -1;
content: "";
position: absolute;
top: 1px;
left: 1px;
right: 1px;
bottom: 1px;
background-color: #fdfed6;
clip-path: polygon(0 0, 88% 0, 100% 50%, 88% 100%, 0 100%);
}
}
}

.slider {
margin-top: -12px;
overflow: hidden;
grid-column: span 1;
background-color: #a09fa4;
}
}
</style>