-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (69 loc) · 1.76 KB
/
index.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
import srraf from 'srraf'
export default function overunder ({ x, x2, y, y2 }) {
let xpos = ''
let ypos = ''
const evs = {}
function on (ev, cb) {
evs[ev] = (evs[ev] || []).concat(cb)
return () => {
evs[ev].splice(evs[ev].indexOf(cb), 1)
}
}
function emit (ev, data) {
(evs[ev] || []).map(cb => cb(data))
}
function getPosition ({
vw: w = window.innerWidth,
y: h = window.pageYOffset
}) {
const _x = x ? x.clientWidth || x : null
const _x2 = x2 ? x2.clientWidth || x2 : null
const _y = y ? y.nodeName ? y.getBoundingClientRect().top + h : y : null
const _y2 = y2 ? y2.nodeName ? y2.getBoundingClientRect().top + h : y2 : null
if (_x && w < _x && xpos !== 'under') {
xpos = 'under'
emit('x under')
} else if (_x2 && w >= _x && w < _x2 && xpos !== 'between') {
xpos = 'between'
emit('x between')
} else if (_x2 && w >= _x2 && xpos !== 'over') {
xpos = 'over'
emit('x over')
} else if (_x && !_x2 && w >= _x && xpos !== 'over') {
xpos = 'over'
emit('x over')
}
if (_y && h < _y && ypos !== 'under') {
ypos = 'under'
emit('y under')
} else if (_y2 && h >= _y && h < _y2 && ypos !== 'between') {
ypos = 'between'
emit('y between')
} else if (_y2 && h >= _y2 && ypos !== 'over') {
ypos = 'over'
emit('y over')
} else if (_y && !_y2 && h >= _y && ypos !== 'over') {
ypos = 'over'
emit('y over')
}
}
const listener = srraf(getPosition)
return {
on,
update ({
x: nx = x,
x2: nx2 = x2,
y: ny = y,
y2: ny2 = y2
} = {}) {
x = nx
x2 = nx2
y = ny
y2 = ny2
listener.update()
},
destroy () {
listener.destroy()
}
}
}