-
Notifications
You must be signed in to change notification settings - Fork 0
/
navbar.js
61 lines (60 loc) · 2.43 KB
/
navbar.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
57
58
59
60
61
export default function navbar() {
// get the current link from the url, and set it to the active link
const url = window.location.pathname;
const activeLink = url.substring(url.lastIndexOf('/') + 1);
if (activeLink === '') {
window.location.href = '/index.html';
}
const navbar = document.querySelector("nav");
const links = navbar.querySelectorAll("a");
function setupSmallNav() {
console.log(links)
const link = [...links].filter(link => link.getAttribute("href") === activeLink)[0];
console.log(link);
if (link.getAttribute("href") === activeLink) {
// disable the transition animation
link.style.transition = "none";
link.classList.add("current");
window.setTimeout(() => {
link.style.transition = "";
}, 0);
// make scroll behavior not smooth
if (navbar.scrollHeight > navbar.clientHeight) {
link.scrollIntoView(true, {
behavior: "auto"
});
navbar.scrollBy(0, -5);
console.log("scroll");
}
link.textContent = link.textContent;
// when the link is clicked or hovered expand the navbar
link.addEventListener("click", e => {
console.log(link.href);
e.preventDefault();
if (navbar.classList.contains("expanded")) {
navbar.classList.remove("expanded");
navbar.style.transitionDuration = "0s";
navbar.style.overflowY = "";
window.setTimeout(() => {
navbar.style.maxHeight = "";
link.scrollIntoView(true, {
behavior: "auto"
});
navbar.scrollBy(0, -5);
navbar.style.transitionDuration = "";
}, 0);
return;
}
navbar.classList.add("expanded");
navbar.style.maxHeight = `min(${navbar.scrollHeight}px, 80vh)`;
window.setTimeout(() => {
navbar.style.overflowY = "auto";
}, parseFloat(window.getComputedStyle(navbar).transitionDuration)*1000);
});
}
}
setupSmallNav();
document.addEventListener("resize", () => {
setupSmallNav();
});
}