-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
·35 lines (31 loc) · 960 Bytes
/
main.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
function createDivs(howMany) {
const body = document.querySelector('body');
for (let i = 0; i < howMany; i++) {
const div = document.createElement('div');
div.innerHTML = i + 1;
div.className = 'invisible';
body.appendChild(div);
}
}
function isVisible(element) {
const topLimit = element.offsetTop;
const bottomLimit = topLimit + element.offsetHeight;
const viewportHeight = window.innerHeight;
const viewportTop = window.scrollY;
const viewportBottom = viewportTop + viewportHeight;
if (viewportTop < topLimit && viewportBottom > bottomLimit) {
return true;
} else {
return false;
}
}
function detectVisibility() {
const divs = document.querySelectorAll('div');
divs.forEach(function(div) {
div.className = isVisible(div) ? 'visible' : 'invisible';
});
}
createDivs(10);
detectVisibility();
window.addEventListener('scroll', detectVisibility);
window.addEventListener('resize', detectVisibility);