-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathutils.js
54 lines (51 loc) · 1.21 KB
/
utils.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
// https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md#feature-detection
let supportsPassive = false
try {
let opts = Object.defineProperty({}, 'passive', {
get: function() {
supportsPassive = true
},
})
window.addEventListener('test', null, opts)
} catch (e) {}
export default {
$(selector) {
if (typeof selector !== 'string') {
return selector
}
return document.querySelector(selector)
},
on(element, events, handler, opts = { passive: false }) {
if (!(events instanceof Array)) {
events = [events]
}
for (let i = 0; i < events.length; i++) {
element.addEventListener(
events[i],
handler,
supportsPassive ? opts : false
)
}
},
off(element, events, handler) {
if (!(events instanceof Array)) {
events = [events]
}
for (let i = 0; i < events.length; i++) {
element.removeEventListener(events[i], handler)
}
},
cumulativeOffset(element) {
let top = 0
let left = 0
do {
top += element.offsetTop || 0
left += element.offsetLeft || 0
element = element.offsetParent
} while (element)
return {
top: top,
left: left,
}
},
}