-
Notifications
You must be signed in to change notification settings - Fork 16
/
vertical.js
48 lines (48 loc) · 1.3 KB
/
vertical.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
module.exports = function (el) {
var windowHeight = window.innerHeight
var elemTop = el.getBoundingClientRect().top
var elemBottom = el.getBoundingClientRect().bottom
var elemHeight = elemBottom - elemTop
if (elemTop > windowHeight) {
// Not viewable, below viewport
return {
value: 0,
state: 'EL_IS_BELOW_VIEW'
}
} else if (elemBottom <= 0) {
// Not viewable, above the viewport
return {
value: 0,
state: 'EL_IS_ABOVE_VIEW'
}
} else if (elemTop >= 0 && elemBottom <= windowHeight) {
// Element is completely visible
return {
value: 1,
state: 'EL_IS_WITHIN_VERTICAL_VIEW'
}
} else if (elemTop < 0 && elemBottom > windowHeight) {
// Top and bottom of element truncated
return {
value: windowHeight / elemHeight,
state: 'EL_BOTTOM_AND_TOP_TRUNCATED'
}
} else if (elemTop < 0 && elemBottom <= windowHeight) {
// Top of element is truncated
return {
value: elemBottom / elemHeight,
state: 'EL_TOP_TRUNCATED'
}
} else if (elemTop >= 0 && elemBottom > windowHeight) {
// Bottom of element is trunctaed
return {
value: (windowHeight - elemTop) / elemHeight,
state: 'EL_BOTTOM_TRUNCATED'
}
}
// Generic error
return {
value: 0,
state: 'EL_IS_NOT_WITHIN_VIEW'
}
}