-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathref-line.js
159 lines (146 loc) · 5.66 KB
/
ref-line.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
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
let lines = {
xt: null,
xc: null,
xb: null,
yl: null,
yc: null,
yr: null,
}
// 置入参考线
for (let p in lines) {
let node = lines[p] = document.createElement('div')
node.classList.add('ref-line', p)
node.style.cssText = `display:none;opacity:0.7;position:absolute;background:#4DAEFF;z-index:199111250;${p[0] === 'x' ? 'width:100%;height:1px;left:0;' : 'width:1px;height:100%;top:0;'}`
// 挂上一些辅助方法
node.show = function () {
this.style.display = 'block'
}
node.hide = function () {
this.style.display = 'none'
}
node.isShow = function () {
return this.style.display !== 'none'
}
document.body.appendChild(node)
}
class RefLine {
constructor(options = {}) {
this.options = Object.assign({
gap: 3
}, options)
}
/**
* @param dragNode {Element} 拖拽元素的原生node
* @param checkNodes {String|Element} 选择器 或者 原生node集合
*/
check(dragNode, checkNodes) {
checkNodes = typeof checkNodes === 'string' ? document.querySelectorAll(checkNodes) : checkNodes
let dragRect = dragNode.getBoundingClientRect()
this.uncheck()
Array.from(checkNodes).forEach((item) => {
item.classList.remove('ref-line-active')
if (item === dragNode) return
let {top, height, bottom, left, width, right} = item.getBoundingClientRect()
let dragWidthHalf = dragRect.width / 2
let itemWidthHalf = width / 2
let dragHeightHalf = dragRect.height / 2
let itemHeightHalf = height / 2
let conditions = {
top: [
// xt-top
{
isNearly : this._isNearly(dragRect.top, top),
lineNode : lines.xt,
lineValue: top,
dragValue: top
},
// xt-bottom
{
isNearly : this._isNearly(dragRect.bottom, top),
lineNode : lines.xt,
lineValue: top,
dragValue: top - dragRect.height
},
// xc
{
isNearly : this._isNearly(dragRect.top + dragHeightHalf, top + itemHeightHalf),
lineNode : lines.xc,
lineValue: top + itemHeightHalf,
dragValue: top + itemHeightHalf - dragHeightHalf
},
// xb-top
{
isNearly : this._isNearly(dragRect.bottom, bottom),
lineNode : lines.xb,
lineValue: bottom,
dragValue: bottom - dragRect.height
},
// xb-bottom
{
isNearly : this._isNearly(dragRect.top, bottom),
lineNode : lines.xb,
lineValue: bottom,
dragValue: bottom
},
],
left: [
// yl-left
{
isNearly : this._isNearly(dragRect.left, left),
lineNode : lines.yl,
lineValue: left,
dragValue: left
},
// yl-right
{
isNearly : this._isNearly(dragRect.right, left),
lineNode : lines.yl,
lineValue: left,
dragValue: left - dragRect.width
},
// yc
{
isNearly : this._isNearly(dragRect.left + dragWidthHalf, left + itemWidthHalf),
lineNode : lines.yc,
lineValue: left + itemWidthHalf,
dragValue: left + itemWidthHalf - dragWidthHalf
},
// yr-left
{
isNearly : this._isNearly(dragRect.right, right),
lineNode : lines.yr,
lineValue: right,
dragValue: right - dragRect.width
},
// yr-right
{
isNearly : this._isNearly(dragRect.left, right),
lineNode : lines.yr,
lineValue: right,
dragValue: right
},
]
}
for (let key in conditions) {
// 遍历符合的条件并处理
conditions[key].forEach((condition) => {
if (!condition.isNearly) return
item.classList.add('ref-line-active')
dragNode.style[key] = `${condition.dragValue}px`
condition.lineNode.style[key] = `${condition.lineValue}px`
condition.lineNode.show()
})
}
})
}
uncheck() {
Object.values(lines).forEach((item) => item.hide())
Array.from(document.querySelectorAll('.ref-line-active')).forEach((item) => item.classList.remove('ref-line-active'))
}
_isNearly(dragValue, targetValue) {
return Math.abs(dragValue - targetValue) <= this.options.gap
}
}
if (typeof module === 'object') {
module.exports = RefLine
}