-
Notifications
You must be signed in to change notification settings - Fork 0
/
scroll.js
56 lines (46 loc) · 1.17 KB
/
scroll.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
const topBtn = document.querySelector(".top-btn");
function scrollUp() {
window.scrollTo({top:0, left:0, behavior:'smooth'});
}
topBtn.addEventListener("click", scrollUp);
window.addEventListener("scroll", (e) => {
e.preventDefault();
if(window.scrollY > 900) {
topBtn.style.display = "block";
topBtn.animate([
{ opacity: "0"},
{ opacity: "1"}
], {
duration: 1000,
easing: "ease",
iterations: 1,
fill: "both"
});
} else {
topBtn.style.display = "none";
}
})
// top scroll up
const navLink = document.querySelectorAll(".navbar-item a");
for (let i = 0; i < navLink.length; i++) {
navLink[i].addEventListener("click", function(e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute("href"));
window.scrollTo ({
'behavior': 'smooth',
'top': target.offsetTop
})
})
}
// nav scroll
function changeBg() {
const navbar = document.querySelector(".navbar");
let scrollValue = window.scrollY;
if (scrollValue < 500) {
navbar.classList.remove("navbg");
} else {
navbar.classList.add("navbg");
}
}
window.addEventListener("scroll", changeBg);
// nav bg color