-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
106 lines (96 loc) · 4.27 KB
/
script.js
File metadata and controls
106 lines (96 loc) · 4.27 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
document.addEventListener("DOMContentLoaded", () => {
const menuButton = document.getElementById("mobile-menu-toggle");
const mobileNavLinksContainer = document.getElementById("mobile-nav-links");
const desktopNavLinksContainer = document.getElementById("desktop-nav-links");
// 1. Populate mobile navigation from desktop navigation
if (desktopNavLinksContainer && mobileNavLinksContainer) {
// Clone the desktop navigation links to the mobile container
const desktopLinksHTML = desktopNavLinksContainer.innerHTML;
mobileNavLinksContainer.innerHTML = desktopLinksHTML;
// Add event listeners to newly created mobile links for closing menu
// and handling smooth scroll (if not handled by CSS scroll-smooth)
mobileNavLinksContainer.querySelectorAll("a").forEach((link) => {
link.addEventListener("click", (e) => {
// Close mobile menu
mobileNavLinksContainer.classList.add("hidden");
mobileNavLinksContainer.classList.remove("active");
// Reset menu icon to 'bars'
const icon = menuButton.querySelector("i");
if (icon && icon.classList.contains("fa-times")) {
icon.classList.remove("fa-times");
icon.classList.add("fa-bars");
}
// Handle smooth scroll for anchor links
const href = link.getAttribute("href");
if (href && href.startsWith("#")) {
// e.preventDefault(); // Prevent default if CSS scroll-smooth is not enough or for more control
const targetElement = document.querySelector(href);
if (targetElement) {
// The html tag has 'scroll-smooth', so browser handles smooth scroll.
// Forcing it via JS might be redundant but can offer more control if needed.
// For now, rely on CSS 'scroll-smooth'.
// If issues arise, uncomment preventDefault and add:
// targetElement.scrollIntoView({ behavior: 'smooth' });
}
}
});
});
}
// 2. Toggle mobile menu visibility and icon
if (menuButton && mobileNavLinksContainer) {
menuButton.addEventListener("click", () => {
mobileNavLinksContainer.classList.toggle("hidden");
mobileNavLinksContainer.classList.toggle("active"); // 'active' class for styling if needed
const icon = menuButton.querySelector("i");
if (icon) {
// Check if menu is now active (visible) to set icon to 'times'
if (mobileNavLinksContainer.classList.contains("active")) {
icon.classList.remove("fa-bars");
icon.classList.add("fa-times");
} else {
icon.classList.remove("fa-times");
icon.classList.add("fa-bars");
}
}
});
}
// 3. Fade-in animations for sections on scroll
const sectionsToFadeIn = document.querySelectorAll(".fade-in");
const observerOptions = {
root: null, // relative to document viewport
rootMargin: "0px", // no margin
threshold: 0.1, // 10% of the item must be visible to trigger
};
const fadeInObserver = new IntersectionObserver(
(entries, observerInstance) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.classList.add("is-visible"); // Add class to trigger CSS transition
observerInstance.unobserve(entry.target); // Stop observing once it's visible
}
});
},
observerOptions
);
sectionsToFadeIn.forEach((section) => {
fadeInObserver.observe(section);
});
// Smooth scroll for all top-level anchor links (for desktop and initial links)
// This ensures that links in the header also smooth scroll.
// The mobile links get this behavior from the cloning logic above.
document
.querySelectorAll('header a[href^="#"], #desktop-nav-links a[href^="#"]')
.forEach((anchor) => {
anchor.addEventListener("click", function (e) {
const href = this.getAttribute("href");
if (href && href.startsWith("#")) {
// HTML has scroll-smooth, so default behavior should be smooth.
// e.preventDefault(); // Only if overriding default behavior
const targetElement = document.querySelector(href);
if (targetElement) {
// targetElement.scrollIntoView({ behavior: 'smooth' }); // Redundant if scroll-smooth on html works
}
}
});
});
});