-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
43 lines (33 loc) · 1.22 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
41
42
43
document.addEventListener("DOMContentLoaded", () => {
const wrapper = document.querySelector(".wrapper");
const navBar = document.querySelector("nav");
const navigationLinks = navBar.querySelectorAll("li");
const hamburgerCheckbox = navBar.querySelector(".peer");
// Hamburger navigation
navigationLinks.forEach((navigationLink) => {
navigationLink.addEventListener("click", () => {
hamburgerCheckbox.checked = false;
});
});
// Navbar appears on scroll down and hides on scroll up
let lastScrollY = document.documentElement.scrollTop;
window.addEventListener("scroll", () => {
document.documentElement.scrollTop - lastScrollY > 0
? hideNavBar()
: showNavBar();
lastScrollY = document.documentElement.scrollTop;
});
function hideNavBar() {
navBar.classList.add("-translate-y-full");
navBar.classList.remove("translate-y-0");
}
function showNavBar() {
navBar.classList.add("translate-y-0");
navBar.classList.remove("-translate-y-full");
}
// Close navigation menu when the dark overlay is clicked on
const darkOverlay = navBar.querySelector(".bg-black");
darkOverlay.addEventListener("click", () => {
hamburgerCheckbox.checked = false;
});
});