-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
44 lines (35 loc) · 1.37 KB
/
script.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
var parallaxdiv = document.getElementsByClassName("parallax")[0];
var parallaxtext = document.getElementById("parallax-text");
var parallaxbutton = document.getElementById("parallax-button");
var textSectionCenter = document.getElementsByClassName("text-section-center")[0];
var textSectionGrid = document.getElementsByClassName("text-section-grid")[0];
function isInViewport(element) {
var rect = element.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.top <= (window.innerHeight || document.documentElement.clientHeight)
);
}
window.addEventListener("scroll", function() {
var scrolledHeight = window.scrollY;
parallaxdiv.style.backgroundPositionY = scrolledHeight * 0.05 + "px";
parallaxtext.style.left = 50 + scrolledHeight * 0.05 + "%";
parallaxbutton.style.right = 44 + scrolledHeight * 0.05 + "%";
// when the text section is in the viewport, start the animation "fromTop"
if (isInViewport(textSectionCenter)) {
textSectionCenter.classList.add("fromTop");
}
if (isInViewport(textSectionGrid)) {
textSectionGrid.classList.add("fromTop");
}
});
setInterval(function() {
// check if we are on top of the page
var scrolledHeight = window.scrollY;
if (scrolledHeight == 0) {
parallaxbutton.style.zIndex = 50;
}
else {
parallaxbutton.style.zIndex = 0;
}
}, 100);