-
Notifications
You must be signed in to change notification settings - Fork 0
/
Drag and drop the sorting.vue
134 lines (126 loc) · 3.2 KB
/
Drag and drop the sorting.vue
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
<template>
<div class="home">
<ul class="list animate" ref="option">
<li
class="listItem"
v-for="(li,idx) in list"
:key="li"
>
{{li}}
<span class="drag" @mousedown="mousedown(li, idx, $event)"></span>
</li>
</ul>
</div>
</template>
<script>
export default {
name: 'home',
data() {
return {
list: [1, 2, 3, 4, 5],
event: 'mousemove'
}
},
methods: {
mousedown(item, index, e) {
let $elem = e.target.parentNode
let $brothers = Array.prototype.slice.call(this.$refs.option.children, 0)
let lastY = e.pageY
let range = [-(index * 40), ($brothers.length -1 - index) * 40]
let target = index
$elem.classList.add('scale')
const mousemove = (ev) => {
let moveY = ev.pageY - lastY
if (moveY < range[0]) moveY = range[0]
if (moveY > range[1]) moveY = range[1]
let tmp = index + Math.round(moveY / 40)
if (tmp !== target) {
$brothers.forEach((it, i) => {
if (i === index) return
if (moveY > 0) {
// 往下走
if (i < index) return
if (i <= tmp) {
it.style.transform = `translateY(-40px)`
} else {
it.style.transform = ''
}
} else {
// 往上走
if (i > index) return
if (i < tmp) {
it.style.transform = ''
} else {
it.style.transform = `translateY(40px)`
}
}
})
target = tmp
}
$elem.style.transform = `translateY(${moveY}px) scale(1.05)`
}
const mouseup = (e) => {
$elem.classList.remove('scale')
$elem.style.transform = `translateY(${40 * (target - index)}px)`
if (target !== index) {
// 临时关闭所有动画
this.$refs.option.classList.remove('animate')
let tmp = this.list.splice(index, 1)[0]
this.list.splice(target, 0, tmp)
// 位移重置
$brothers.forEach(it => {
it.style.transform = ''
})
this.$nextTick(() => {
this.$refs.option.classList.add('animate')
})
}
document.removeEventListener('mousemove', mousemove)
document.removeEventListener('mouseup', mouseup)
}
document.addEventListener('mousemove', mousemove)
document.addEventListener('mouseup', mouseup)
},
}
}
</script>
<style>
.scale{
transform: scale(1.05);
box-shadow: 0 0 8px rgba(0, 0, 0, 0.15);
}
.home{
width: 400px;
margin: 0 auto;
}
.list{
list-style: none;
padding: 0;
width: 100%;
}
.listItem{
border: 1px solid pink;
height: 40px;
line-height: 40px;
width: 100%;
position: relative;
user-select: none;
}
.animate .listItem{
transition: transform 0.1s linear;
}
.listItem:nth-of-type(2n){
border-top: none;
border-bottom: none;
}
.drag{
border: 1px solid pink;
width: 20px;
height: 20px;
display: inline-block;
position: absolute;
right: 10px;
top: 10px;
cursor: pointer;
}
</style>