-
Notifications
You must be signed in to change notification settings - Fork 0
/
real-visibility.js
58 lines (53 loc) · 1.42 KB
/
real-visibility.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
/**
* LICENSE: The MIT License
*/
Element.prototype.isReallyNear = function(otherEl) {
let el = this;
if (!el || !otherEl) {
return null;
}
return el.contains(otherEl) || otherEl.contains(el);
};
Element.prototype.isReallyVisible = function() {
let el = this;
let debug = false;
if (!document.body.contains(el)) {
return null;
}
if (document.hidden===true) {
return false;
} else if (document.hidden!==false) {
console.warn("NOT FATAL WARNING: Page Visibility API is not supported by your browser.");
}
let rectPos = el.getBoundingClientRect();
if (debug) {
console.debug("Bounding of: ", el, rectPos);
}
let result = 0;
if (el.isReallyNear(document.elementFromPoint(rectPos.left, rectPos.top))) {
result++;
}
if (el.isReallyNear(document.elementFromPoint(rectPos.left, rectPos.bottom - 1))) {
result++;
}
if (el.isReallyNear(document.elementFromPoint(rectPos.right - 1, rectPos.top))) {
result++;
}
if (el.isReallyNear(document.elementFromPoint(rectPos.right - 1, rectPos.bottom - 1))) {
result++;
}
let info;
let r = true;
if (result == 4) {
info = 'visible';
} else if (result === 0) {
info = 'hidden';
r = false;
} else {
info = 'partially visible';
}
if (debug) {
console.debug(info, el);
}
return r;
};