-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.html
156 lines (140 loc) · 4.34 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>拖动排序</title>
<style>
body {
margin: 0;
padding: 0;
}
.box {
display: flex;
font-size: 15px;
}
.items {
margin: 20px 30px;
position: relative;
background-color: #f6f9fa;
overflow: hidden;
border-radius: 4px;
}
.item {
width: 100px;
height: 32px;
line-height: 32px;
text-align: center;
transition: background-color .3s, color .3s, box-shadow .5s;
cursor: pointer;
user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-webkit-user-select: none;
}
.item:hover {
background-color: #00a1d6;
color: #fff;
}
.select {
position: absolute;
background-color: #00a1d6;
color: #fff;
box-shadow: 0 4px 4px #ccc;
z-index: 999;
}
.hold {
cursor: inherit;
border: 1px dashed #ccc;
box-sizing: border-box;
animation: hold-higther 200ms ease-in;
}
@keyframes hold-higther {
0% {
transform: scale(.4);
}
100% {
transform: scale(1);
}
}
</style>
</head>
<body>
<div class="box"></div>
<script>
const $ = selectors => document.querySelector(selectors);
const $$ = selectors => document.querySelectorAll(selectors);
const navList = ['直播', '动画', '番剧', '漫画', '游戏', '舞蹈', '数码', '知识', '生活', '美食', '纪律片', '电影'];
const holdItemDom = document.createElement('div');
holdItemDom.classList.add('item', 'hold');
const listDom = document.createElement('div');
listDom.classList.add('items');
navList.forEach((v, i, arr) => {
const itemDom = document.createElement('div');
itemDom.classList.add('item');
itemDom.dataset.sortindex = i;
itemDom.textContent = v;
listDom.appendChild(itemDom);
});
$('.box').appendChild(listDom);
//主要代码
var dragObj;
//考虑到item数量会很多,不需要给每个item单独绑定mousedown,mousemove,mouseup事件
listDom.onmousedown = (event) => {
let currenIndex, previousIndex;
const itemDomList = $$('.items .item');
const listLength = navList.length;
const startY = event.clientY;
//找到mousedown的item
let selectIndex;
dragObj = event.target;
Array.from(itemDomList).forEach((v, i) => {
if (v === event.target) {
selectIndex = i;
}
});
dragObj.after(holdItemDom);
dragObj.classList.add('select');
dragObj.style.top = dragObj.clientHeight * selectIndex + 'px';
const itemHeight = dragObj.clientHeight;
let topIndex = selectIndex;
const startTop = dragObj.style.top;
previousIndex = Math.ceil((parseInt(startTop) - itemHeight / 2) / itemHeight);
document.onmousemove = function (event) {
dragObj.classList.add('select');
const moveY = event.clientY;
const presentTop = parseInt(startTop) + (moveY - startY);
dragObj.style.top = presentTop + 'px';
currenIndex = Math.ceil((presentTop - itemHeight / 2) / itemHeight);
if (currenIndex < 0) currenIndex = 0;
if (currenIndex > listLength - 1) currenIndex = listLength - 1;
if (previousIndex !== currenIndex) {
holdItemDom.remove();
if (previousIndex > currenIndex && currenIndex < topIndex) {
dragObj.remove();
itemDomList[currenIndex].before(dragObj);
dragObj.after(holdItemDom);
} else {
const currentItems = $$('.box .item');
currentItems[currenIndex].after(holdItemDom);
}
previousIndex = currenIndex;
if (currenIndex < topIndex) topIndex = currenIndex;
}
};
document.onmouseup = function (event) {
const currentItems = $$('.box .item');
if (currenIndex > topIndex) {
currentItems[currenIndex].after(dragObj);
}
holdItemDom.remove();
dragObj.classList.remove('select');
dragObj.removeAttribute('style');
document.onmousemove = null;
document.onmouseup = null;
}
};
</script>
</body>
</html>