-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscroll.js
49 lines (44 loc) · 1.5 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
const navbar = document.querySelector(".navbar");
const tourbtn = document.querySelector(".tour_btn");
const linksContainer = document.querySelector(".links_container");
const topLink = document.querySelector(".top-link");
// date
const date = document.getElementById("date");
date.innerHTML = new Date().getFullYear();
// fixed navbar
window.addEventListener("scroll", () => {
const scrollHeight = window.pageYOffset;
const navHeight = navbar.getBoundingClientRect().height;
scrollHeight > navHeight
? navbar.classList.add("fixed_nav")
: navbar.classList.remove("fixed_nav");
height > 500
? topLink.classList.add("show-link")
: topLink.classList.remove("show-link");
});
// smooth scroll
const scrollLink = document.querySelectorAll(".page_link");
console.log(scrollLink);
scrollLink.forEach((link) => {
link.addEventListener("click", (e) => {
// prevent default
e.preventDefault();
// navigate to specific spot
const id = e.currentTarget.getAttribute("href").slice(1);
const element = document.getElementById(id);
const navHeight = navbar.getBoundingClientRect().height;
const containerHieght = linksContainer.getBoundingClientRect().height;
const fixedNav = navbar.classList.contains("fixed_nav");
let position = element.offsetTop - navHeight;
if (!navHeight) {
position = position - navHeight;
}
if (navHeight > 82) {
position = position + containerHieght;
}
window.scrollTo({
left: 0,
top: position,
});
});
});