|
| 1 | +document.addEventListener("DOMContentLoaded", function () { |
| 2 | + const nav = document.querySelector(".nav-container"); |
| 3 | + const heroSection = document.querySelector("#hero"); |
| 4 | + const navCheckbox = document.getElementById("nav-toggle"); |
| 5 | + |
| 6 | + // Basic sticky navigation |
| 7 | + const handleScroll = () => { |
| 8 | + const scrollPosition = window.scrollY; |
| 9 | + if (scrollPosition > heroSection.offsetHeight) { |
| 10 | + nav.classList.add("sticky"); |
| 11 | + } else { |
| 12 | + nav.classList.remove("sticky"); |
| 13 | + } |
| 14 | + }; |
| 15 | + |
| 16 | + // Clean navigation to sections |
| 17 | + const handleNavClick = (e) => { |
| 18 | + if (!e.target.matches("a")) return; |
| 19 | + |
| 20 | + e.preventDefault(); |
| 21 | + const targetId = e.target.getAttribute("href"); |
| 22 | + const targetSection = document.querySelector(targetId); |
| 23 | + |
| 24 | + if (!targetSection) return; |
| 25 | + |
| 26 | + // Close menu |
| 27 | + navCheckbox.checked = false; |
| 28 | + |
| 29 | + // Calculate proper offset including nav height |
| 30 | + const navHeight = nav.offsetHeight; |
| 31 | + const offset = targetId === "#contact" ? navHeight : navHeight + 32; |
| 32 | + |
| 33 | + // Smooth scroll with adjusted position |
| 34 | + window.scrollTo({ |
| 35 | + top: targetSection.offsetTop - offset, |
| 36 | + behavior: "smooth", |
| 37 | + }); |
| 38 | + |
| 39 | + // Update URL without jumping |
| 40 | + window.history.pushState(null, "", targetId); |
| 41 | + }; |
| 42 | + |
| 43 | + // Event listeners |
| 44 | + window.addEventListener("scroll", handleScroll); |
| 45 | + document.querySelector(".dropdown").addEventListener("click", handleNavClick); |
| 46 | + |
| 47 | + // Close menu when clicking outside |
| 48 | + document.addEventListener("click", (e) => { |
| 49 | + if (!nav.contains(e.target)) { |
| 50 | + navCheckbox.checked = false; |
| 51 | + selectButton.classList.add("blinking"); |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + // Add blinking state management |
| 56 | + const selectButton = document.querySelector(".select-button"); |
| 57 | + |
| 58 | + selectButton.addEventListener("mouseenter", () => { |
| 59 | + selectButton.classList.remove("blinking"); |
| 60 | + }); |
| 61 | + |
| 62 | + selectButton.addEventListener("mouseleave", () => { |
| 63 | + if (!navCheckbox.checked) { |
| 64 | + selectButton.classList.add("blinking"); |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + document.querySelector(".dropdown").addEventListener("click", (e) => { |
| 69 | + if (e.target.matches("a")) { |
| 70 | + // Resume blinking after navigation |
| 71 | + setTimeout(() => { |
| 72 | + selectButton.classList.add("blinking"); |
| 73 | + }, 100); |
| 74 | + } |
| 75 | + }); |
| 76 | +}); |
0 commit comments