-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdrag.js
50 lines (50 loc) · 1.9 KB
/
drag.js
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
Vue.directive('drag', {
inserted (el, binding) {
const dragCells = Array.from(el.tHead.rows[0].cells);
const margin = binding.value || 10;
const positionMark = {
clientX: 0, // 在单元格右侧点击时的clientX
clientOldWidth: 0, // 所点击的单元格的宽度
mousedown: false, // 判断鼠标是否处于mousedown
target: null, // 点击的元素
}
for (const [index, th] of dragCells.entries()) {
if (index !== dragCells.length - 1) {
th.addEventListener('mousedown', function(e) {
if (e.offsetX > e.target.clientWidth - margin) {
for (const value of dragCells) {
if (value !== dragCells[index + 1]) {
value.width = value.clientWidth; // 设置除下一节点外的节点宽度
}
}
dragCells[index + 1].width = ''; // 移除下一节点宽度
positionMark.clientX = e.clientX;
positionMark.mousedown = true;
positionMark.clientOldWidth = e.target.clientWidth;
positionMark.target = th;
}
});
th.addEventListener('mousemove', function(e) {
if (e.offsetX > th.clientWidth - margin) {
th.style.cursor = 'col-resize';
} else {
th.style.cursor = '';
}
});
}
}
document.addEventListener('mouseup', function (e) {
if (positionMark.mousedown) {
const nextTh = positionMark.target.nextElementSibling;
nextTh.width = nextTh.clientWidth; // 设置下一节点的节点宽度
positionMark.mousedown = false;
}
});
el.addEventListener('mousemove', function (e) {
if (positionMark.mousedown) {
const width = positionMark.clientOldWidth + (e.clientX - positionMark.clientX);
positionMark.target.width = Math.max(1, width);
}
});
}
});