// Mobile Navigation and Smooth Scrolling document.addEventListener('DOMContentLoaded', function() { // Mobile Menu Toggle Functionality const mobileMenuToggle = document.querySelector('.mobile-menu-toggle'); const mobileNavOverlay = document.querySelector('.mobile-nav-overlay'); const mobileNavLinks = document.querySelectorAll('.mobile-nav-link'); const body = document.body; // Toggle mobile menu function toggleMobileMenu() { const isActive = mobileMenuToggle.classList.contains('active'); if (isActive) { closeMobileMenu(); } else { openMobileMenu(); } } function openMobileMenu() { mobileMenuToggle.classList.add('active'); mobileNavOverlay.classList.add('active'); mobileMenuToggle.setAttribute('aria-expanded', 'true'); mobileNavOverlay.setAttribute('aria-hidden', 'false'); body.style.overflow = 'hidden'; // Prevent background scroll } function closeMobileMenu() { mobileMenuToggle.classList.remove('active'); mobileNavOverlay.classList.remove('active'); mobileMenuToggle.setAttribute('aria-expanded', 'false'); mobileNavOverlay.setAttribute('aria-hidden', 'true'); body.style.overflow = ''; // Restore scrolling } // Mobile menu toggle button event if (mobileMenuToggle) { mobileMenuToggle.addEventListener('click', toggleMobileMenu); } // Close mobile menu when clicking on overlay background if (mobileNavOverlay) { mobileNavOverlay.addEventListener('click', function(e) { if (e.target === mobileNavOverlay) { closeMobileMenu(); } }); } // Close mobile menu when clicking nav links mobileNavLinks.forEach(link => { link.addEventListener('click', function(e) { // If it's an anchor link (not external), close menu after brief delay if (this.getAttribute('href').startsWith('#')) { setTimeout(() => { closeMobileMenu(); }, 100); } else { closeMobileMenu(); } }); }); // Close mobile menu on escape key document.addEventListener('keydown', function(e) { if (e.key === 'Escape' && mobileNavOverlay.classList.contains('active')) { closeMobileMenu(); } }); // Close mobile menu on window resize to desktop size window.addEventListener('resize', function() { if (window.innerWidth > 767 && mobileNavOverlay.classList.contains('active')) { closeMobileMenu(); } }); // Smooth scrolling for all navigation links const allNavLinks = document.querySelectorAll('nav a[href^="#"], .mobile-nav-link[href^="#"]'); allNavLinks.forEach(link => { link.addEventListener('click', function(e) { e.preventDefault(); const targetId = this.getAttribute('href'); const targetSection = document.querySelector(targetId); if (targetSection) { targetSection.scrollIntoView({ behavior: 'smooth', block: 'start' }); } }); }); // Highlight active navigation item based on scroll position function updateActiveNav() { const sections = document.querySelectorAll('section[id]'); const navLinks = document.querySelectorAll('nav a[href^="#"], .mobile-nav-link[href^="#"]'); let current = ''; sections.forEach(section => { const sectionTop = section.offsetTop; const sectionHeight = section.clientHeight; if (pageYOffset >= sectionTop - 200) { current = section.getAttribute('id'); } }); navLinks.forEach(link => { link.classList.remove('active'); if (link.getAttribute('href') === '#' + current) { link.classList.add('active'); } }); } window.addEventListener('scroll', updateActiveNav); });