-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
62 lines (50 loc) · 2.16 KB
/
app.js
File metadata and controls
62 lines (50 loc) · 2.16 KB
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
62
document.addEventListener("DOMContentLoaded", function () {
const navbarLinks = document.querySelectorAll(".nav-links a");
const footerLinks = document.querySelectorAll(".footer-links a");
const navbarHeight = 50;
const allLinks = [...navbarLinks, ...footerLinks];
allLinks.forEach(link => {
link.addEventListener("click", function (e) {
smoothScroll(e, allLinks);
});
});
function smoothScroll(e, links) {
e.preventDefault();
const targetHref = e.target.getAttribute("href");
if (targetHref.startsWith("#")) {
const targetId = targetHref.slice(1);
const targetSection = document.getElementById(targetId);
if (targetSection) {
const targetPosition = targetSection.offsetTop - navbarHeight;
window.scrollTo({
top: targetPosition,
behavior: "smooth"
});
links.forEach(nav => nav.classList.remove("active"));
e.target.classList.add("active");
}
}
}
});
function toggleMenu() {
const navLinks = document.querySelector('.nav-links');
const hamburgerButton = document.querySelector('.hamburger-button');
// Toggle the 'open' class for nav links
navLinks.classList.toggle('open');
// Update the hamburger icon based on state
if (navLinks.classList.contains('open')) {
hamburgerButton.innerHTML = '✕'; // Change to 'X' when open
} else {
hamburgerButton.innerHTML = '☰'; // Change back to hamburger icon
}
}
// Close the menu when clicking outside
document.addEventListener('click', (event) => {
const navLinks = document.querySelector('.nav-links');
const hamburgerButton = document.querySelector('.hamburger-button');
// Check if the click is outside the menu and hamburger button
if (!navLinks.contains(event.target) && !hamburgerButton.contains(event.target)) {
navLinks.classList.remove('open');
hamburgerButton.innerHTML = '☰'; // Reset to hamburger icon
}
});