-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
55 lines (48 loc) · 1.64 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
44
45
46
47
48
49
50
51
52
53
54
55
document.addEventListener("DOMContentLoaded", () => {
const burgerIcon = document.getElementById("burger-icon");
const navList = document.getElementById("nav-list");
const header = document.querySelector("header");
const navLinks = document.querySelectorAll(".nav-link, .dropdown-link");
// Toggle mobile menu
burgerIcon.addEventListener("click", () => {
navList.classList.toggle("show");
burgerIcon.classList.toggle("active");
});
// Close mobile menu when clicking outside
document.addEventListener("click", (event) => {
if (!navList.contains(event.target) && !burgerIcon.contains(event.target)) {
navList.classList.remove("show");
burgerIcon.classList.remove("active");
}
});
// Smooth scrolling for nav links
navLinks.forEach((link) => {
link.addEventListener("click", (e) => {
e.preventDefault();
const targetId = link.getAttribute("href");
const targetElement = document.querySelector(targetId);
if (targetElement) {
const headerHeight = header.offsetHeight;
const targetPosition =
targetElement.getBoundingClientRect().top +
window.pageYOffset -
headerHeight;
window.scrollTo({
top: targetPosition,
behavior: "smooth",
});
// Close mobile menu after clicking a link
navList.classList.remove("show");
burgerIcon.classList.remove("active");
}
});
});
// Add shadow to header on scroll
window.addEventListener("scroll", () => {
if (window.scrollY > 0) {
header.classList.add("scrolled");
} else {
header.classList.remove("scrolled");
}
});
});