-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
32 lines (27 loc) · 1002 Bytes
/
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
let paragraphs = [...document.querySelectorAll('p')];
let spans = [];
paragraphs.forEach(paragraph => {
let htmlString = '';
let pArray = paragraph.textContent.split(' ');
for(let i=0; i<pArray.length; i++){
htmlString += `<span>${pArray[i]}</span> `;
}
paragraph.innerHTML = htmlString;
});
spans = [...document.querySelectorAll('span')];
function revealSpans() {
for(let i=0; i<spans.length; i++) {
if(spans[i].parentElement.getBoundingClientRect().top < window.innerHeight/2) {
let {left, top} = spans[i].getBoundingClientRect();
top = top - (window.innerHeight * 0.2);
let opacityValue = 1 - ((top * 0.01) + (left * 0.001));
opacityValue = opacityValue < 0.1 ? 0.1 : opacityValue;
opacityValue = opacityValue > 1 ? 1 : opacityValue;
spans[i].style.opacity = opacityValue.toFixed(3);
}
}
}
window.addEventListener('scroll', () => {
revealSpans();
});
revealSpans();