Skip to content

Commit 8845480

Browse files
committed
Refactor navigation functionality by moving sticky behavior to main.js and removing inline script from index.html
1 parent a5954d2 commit 8845480

2 files changed

Lines changed: 35 additions & 16 deletions

File tree

index.html

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,21 +139,6 @@ <h2 class="text-2xl md:text-3xl lg:text-4xl font-bold">
139139
<footer class="text-center py-6 bg-gray-100 mt-8">
140140
<p class="text-gray-600">Josue Zazueta LLC (c)</p>
141141
</footer>
142-
<script>
143-
document.addEventListener("DOMContentLoaded", function () {
144-
const nav = document.querySelector(".nav-container");
145-
const heroSection = document.querySelector("#hero");
146-
const navOffset = nav.offsetTop;
147-
148-
window.addEventListener("scroll", () => {
149-
const scrollPosition = window.scrollY + 16; // Adding 16px buffer
150-
if (scrollPosition > heroSection.offsetHeight) {
151-
nav.classList.add("sticky");
152-
} else {
153-
nav.classList.remove("sticky");
154-
}
155-
});
156-
});
157-
</script>
142+
<script src="js/main.js"></script>
158143
</body>
159144
</html>

js/main.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
document.addEventListener("DOMContentLoaded", function () {
2+
const nav = document.querySelector(".nav-container");
3+
const heroSection = document.querySelector("#hero");
4+
const navOffset = nav.offsetTop;
5+
const navCheckbox = document.getElementById("nav-toggle");
6+
const dropdown = document.querySelector(".dropdown");
7+
8+
// Sticky navigation handling
9+
window.addEventListener("scroll", () => {
10+
const scrollPosition = window.scrollY + 16;
11+
if (scrollPosition > heroSection.offsetHeight) {
12+
nav.classList.add("sticky");
13+
} else {
14+
nav.classList.remove("sticky");
15+
}
16+
});
17+
18+
// Function to close the menu
19+
const closeMenu = () => {
20+
navCheckbox.checked = false;
21+
};
22+
23+
// Close menu when clicking links
24+
dropdown.querySelectorAll("a").forEach((link) => {
25+
link.addEventListener("click", closeMenu);
26+
});
27+
28+
// Close menu when clicking outside
29+
document.addEventListener("click", (e) => {
30+
if (!nav.contains(e.target)) {
31+
closeMenu();
32+
}
33+
});
34+
});

0 commit comments

Comments
 (0)