|
64 | 64 | const navbar = document.querySelector('.navbar'); |
65 | 65 |
|
66 | 66 | if (navIcon && navMenu) { |
| 67 | + // Set initial state - only on mobile |
| 68 | + const isMobile = window.innerWidth <= 991; |
| 69 | + |
| 70 | + if (isMobile) { |
| 71 | + navMenu.style.maxHeight = '0'; |
| 72 | + navMenu.style.overflow = 'hidden'; |
| 73 | + navMenu.style.opacity = '0'; |
| 74 | + } |
| 75 | + |
| 76 | + navMenu.style.transition = 'max-height 0.4s ease-out, opacity 0.3s ease-out'; |
| 77 | + |
67 | 78 | navIcon.addEventListener('click', function(e) { |
68 | 79 | e.preventDefault(); |
69 | 80 | e.stopPropagation(); |
70 | 81 |
|
71 | | - // Toggle menu visibility |
72 | | - if (navMenu.style.display === 'flex' || navMenu.style.display === 'block') { |
73 | | - navMenu.style.display = 'none'; |
| 82 | + // Toggle menu visibility with slide animation |
| 83 | + const isOpen = navbar.classList.contains('open'); |
| 84 | + |
| 85 | + if (isOpen) { |
| 86 | + // Close: slide up |
| 87 | + navMenu.style.maxHeight = '0'; |
| 88 | + navMenu.style.opacity = '0'; |
| 89 | + setTimeout(function() { |
| 90 | + navMenu.style.overflow = 'hidden'; |
| 91 | + }, 400); // Wait for animation to complete |
74 | 92 | navbar.classList.remove('open'); |
75 | 93 | } else { |
| 94 | + // Open: slide down |
76 | 95 | navMenu.style.display = 'flex'; |
| 96 | + navMenu.style.overflow = 'hidden'; // Keep hidden during animation |
| 97 | + // Use a large fixed height for smooth animation |
| 98 | + navMenu.style.maxHeight = '2000px'; |
| 99 | + navMenu.style.opacity = '1'; |
| 100 | + setTimeout(function() { |
| 101 | + navMenu.style.overflow = 'visible'; // Allow dropdowns to show |
| 102 | + }, 400); |
77 | 103 | navbar.classList.add('open'); |
78 | 104 | } |
79 | 105 | }); |
80 | 106 |
|
81 | 107 | // Close menu when clicking outside |
82 | 108 | document.addEventListener('click', function(e) { |
83 | 109 | if (!e.target.closest('.navbar')) { |
84 | | - navMenu.style.display = 'none'; |
| 110 | + const isMobile = window.innerWidth <= 991; |
| 111 | + if (isMobile) { |
| 112 | + navMenu.style.maxHeight = '0'; |
| 113 | + navMenu.style.opacity = '0'; |
| 114 | + navMenu.style.overflow = 'hidden'; |
| 115 | + } |
| 116 | + navbar.classList.remove('open'); |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + // Handle window resize |
| 121 | + window.addEventListener('resize', function() { |
| 122 | + const isMobile = window.innerWidth <= 991; |
| 123 | + if (!isMobile) { |
| 124 | + // Reset styles on desktop |
| 125 | + navMenu.style.maxHeight = ''; |
| 126 | + navMenu.style.opacity = ''; |
| 127 | + navMenu.style.overflow = ''; |
85 | 128 | navbar.classList.remove('open'); |
| 129 | + } else if (!navbar.classList.contains('open')) { |
| 130 | + // Ensure menu is hidden on mobile |
| 131 | + navMenu.style.maxHeight = '0'; |
| 132 | + navMenu.style.opacity = '0'; |
| 133 | + navMenu.style.overflow = 'hidden'; |
86 | 134 | } |
87 | 135 | }); |
88 | 136 | } |
|
0 commit comments