Skip to content

Commit 6aed32b

Browse files
committed
added smoother drop down
1 parent a6d4028 commit 6aed32b

2 files changed

Lines changed: 60 additions & 4 deletions

File tree

scripts/simple-dropdown.js

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,73 @@
6464
const navbar = document.querySelector('.navbar');
6565

6666
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+
6778
navIcon.addEventListener('click', function(e) {
6879
e.preventDefault();
6980
e.stopPropagation();
7081

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
7492
navbar.classList.remove('open');
7593
} else {
94+
// Open: slide down
7695
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);
77103
navbar.classList.add('open');
78104
}
79105
});
80106

81107
// Close menu when clicking outside
82108
document.addEventListener('click', function(e) {
83109
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 = '';
85128
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';
86134
}
87135
});
88136
}

styles/custom-nav.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@
4141
min-width: 0;
4242
}
4343

44+
/* Smooth hamburger menu animation - mobile only */
45+
@media (max-width: 991px) {
46+
.nav-menu {
47+
transition: max-height 0.4s ease-out, opacity 0.3s ease-out;
48+
will-change: max-height, opacity;
49+
}
50+
}
51+
4452
/* Hide stray navigation elements */
4553
.nav-icon-line { display: block !important; }
4654

0 commit comments

Comments
 (0)