Corey Klemow
Coder
I'm building a clickable responsive dropdown navbar menu with CSS and vanilla javascript. It's 99.99% working - probably well enough to use - but something weird is going on under the hood. I want the dropdowns to animate in and out, and it's working fine in expanded mode (classes including max-height: 0/overflow-hidden/transitions and max-height:100% are applied on click via JS code) - but in hamburger mode, the animation doesn't happen; it just pauses for the length of the transition before appearing or disappearing.
The dropdown containers are currently divs. If I change them to li, it fixes the problem... but creates a worse problem by breaking the positioning of the dropdowns in expanded mode - they both align under the leftmost menu item. (There's something seriously goony about the dropdown positioning; for reasons I don't understand, I had to change them to position:fixed instead of absolute to get them to line up with their top menu items, as well as having to position them top:60px because they were appearing flush with the top of the page (the navbar is a fixed height of 60px). All of which might have to do with the fact that I had to change the menu item container to a flexbox in the expanded mode to get the top-level menu items to lay out properly at all and I'm new to flexboxes. Though I also needed it to be a flexbox for aesthetic reasons, so I could juggle the order of the links in expanded mode.)
Here's a demo where the dropdown container is a div and here's a demo where the dropdown container is a li. Code below.
Menu HTML:
Menu CSS (sorry to post so much of it, but I honestly don't know where the problem is):
The dropdown containers are currently divs. If I change them to li, it fixes the problem... but creates a worse problem by breaking the positioning of the dropdowns in expanded mode - they both align under the leftmost menu item. (There's something seriously goony about the dropdown positioning; for reasons I don't understand, I had to change them to position:fixed instead of absolute to get them to line up with their top menu items, as well as having to position them top:60px because they were appearing flush with the top of the page (the navbar is a fixed height of 60px). All of which might have to do with the fact that I had to change the menu item container to a flexbox in the expanded mode to get the top-level menu items to lay out properly at all and I'm new to flexboxes. Though I also needed it to be a flexbox for aesthetic reasons, so I could juggle the order of the links in expanded mode.)
Here's a demo where the dropdown container is a div and here's a demo where the dropdown container is a li. Code below.
Menu HTML:
HTML:
<nav>
<div class="logo">
<a class="ourname" href="#">
<img src="http://www.sacredfools.org/images/2d-logo-white-on-black-500px-128x128.png">
SACRED FOOLS</a></div>
<input type="checkbox" id="menutoggler" class="toggler">
<div class="hamburger"><div></div></div>
<ul class="menu__box">
<li><a class="menu__item" href="#">TICKETS</a></li>
<li><a class="menu__item shows__dropbtn" id="shows__button" onclick="shows__dropdown()">SHOWS
<i class="fa-solid fa-caret-down"></i></a>
<li class="shows__dropdownhider" id="shows__dropdowncontainer">
<a class="menu__dropdownitem" href="#">All Shows</a>
<a class="menu__dropdownitem" href="#">Calendar</a>
<a class="menu__dropdownitem menu__lastdropdown" href="#">Past Productions</a>
</li></li>
<li><a class="menu__item more__dropbtn" id="more__button" onclick="more__dropdown()">MORE
<i class="fa-solid fa-caret-down"></i></a>
<li class="more__dropdownhider" id="more__dropdowncontainer">
<a class="menu__dropdownitem" href="#">Directions</a>
<a class="menu__dropdownitem" href="#">Members & Staff</a>
<a class="menu__dropdownitem" href="#">Get Involved</a>
<a class="menu__dropdownitem" href="#">About</a>
<a class="menu__dropdownitem" href="#">Mission</a>
<a class="menu__dropdownitem" href="#">Anti-Racism</a>
<a class="menu__dropdownitem" href="#">EDI</a>
<a class="menu__dropdownitem" href="#">Education & Outreach</a>
<a class="menu__dropdownitem" href="#">Press</a>
<a class="menu__dropdownitem" href="#">Awards</a>
<a class="menu__dropdownitem" href="#">Contact Us</a>
<a class="menu__dropdownitem" href="#">Mailing List</a>
<a class="menu__dropdownitem menu__lastdropdown" href="#">Search</a>
</li></li>
<li><a class="menu__item menu__highlight menu__lastitem" href="#">DONATE</a></li>
<li><a class="menu__item menu__social" href="#"><i class="fa-brands fa-square-facebook"></i></a></li>
<li><a class="menu__item menu__social" href="#"><i class="fa-brands fa-square-twitter"></i></a></li>
<li><a class="menu__item menu__social" href="#"><i class="fa-brands fa-square-instagram"></i></a></li>
<li class="menu__overflowspacer"></li>
</ul>
</nav>
Menu CSS (sorry to post so much of it, but I honestly don't know where the problem is):
CSS:
nav {background-color: #282828;
padding: 0; margin: 0;
position: fixed; top: 0; left: 0;
width: 100%; z-index: 2;
}
nav, .menuspacer {height: 60px}
.logo {color: #ffffff; font-size: 12pt; position: fixed; top: 0; left: 0;
font-family: 'Montserrat', sans-serif; font-weight: bold; z-index: 3;
text-shadow: 2px 2px 6px rgba(0, 0, 0, 0.7);
}
.logo img {height: 60px; margin-left: 0px; margin-right: 5px; margin-top: 0px; margin-bottom: 0px;
vertical-align:middle;}
a.ourname:link, a.ourname:visited, a.ourname:hover, a.ourname:active
{text-decoration: none; color: #ffffff;}
.toggler{
z-index:2;
height: 50px;
width: 50px;
position: absolute;
top: 0;
right: 0;
cursor: pointer;
opacity: 0;
}
.hamburger{
position: absolute;
top: 0;
right: 0;
height: 40px;
width: 40px;
padding: 0.6rem;
display: flex;
align-items: center;
justify-content: center;
}
.hamburger > div{
position: relative;
top: 0;
left: 0;
background: white;
height: 2px;
width: 60%;
transition: all 0.4s ease;
}
.hamburger > div::before,
.hamburger > div::after{
content: '';
position: absolute;
top: -8px;
background: white;
width: 100%;
height: 2px;
transition: all 0.4s ease;
}
.hamburger > div::after{
top: 8px;
}
.toggler:checked + .hamburger > div{
background: rgba(0,0,0,0);
}
.toggler:checked + .hamburger > div::before{
top: 0;
transform: rotate(45deg);
background: white;
}
.toggler:checked + .hamburger > div::after{
top: 0;
transform: rotate(135deg);
background: white;
}
.toggler:checked ~ .menu__box{
left: 0px;
}
.menu__box {
position: fixed;
top: 60px;
left: -100%;
width: 280px;
height: 100%;
min-height: 100%;
box-sizing: border-box;
margin: 0px;
padding-left: 0;
list-style: none;
background-color: #282828;
transition: all 0.4s ease;
overflow: auto;
z-index: 1;
}
.menu__item {
display: block; float: none;
padding: 11px;
color: #ffffff;
font-family: 'Open Sans', sans-serif;
font-size: 12pt;
font-weight: 600;
text-decoration: none;
transition-duration: .25s;
border-top: 1px dotted #ffffff;
}
.menu__dropbox {list-style: none;}
.menu__dropdownitem {
display: block;
padding-top: 5px;
padding-left: 30px;
padding-right: 11px;
padding-bottom: 5px;
color: #ffffff;
font-family: 'Open Sans', sans-serif;
font-size: 12pt;
font-weight: 600;
text-decoration: none;
transition-duration: .25s;
}
.shows__dropdownhider, .more__dropdownhider {max-height: 0; overflow: hidden;
-webkit-transition-duration: .25s;
-moz-transition-duration: .25s;
-o-transition-duraton: .25s;
transition-duration: .25s;}
.menu__dropdownshow {max-height: 100%;}
.shows__dropbtn, .more__dropbtn {cursor: pointer;}
.menu__lastdropdown {padding-bottom: 11px}
.menu__lastitem {border-bottom: 1px dotted #ffffff;}
.menu__item:hover, .menu__item:active,
.menu__dropdownitem:hover, .menu__dropdownitem:active {color: #FB0901}
.menu__item:hover, .menu__item:active,
.menu__dropdownitem:hover, .menu__dropdownitem:active {background-color: #000000}
.menu__highlight {background-color: #404040}
.menu__social {float: left; border: none; font-size: 20pt}
.menu__social:hover, .menu__social:active {background-color: transparent}
.menu__overflowspacer {height: 120px}
.fa-caret-down {margin: 5px; font-size: 8pt; float: right}
.menu__toggledon {color: #FB0901; background-color: #000000;}
@media (min-width: 768px) {
.hamburger {display: none}
.toggler:checked ~ .menu__box{
left: auto;
}
.menu__box {
display: flex;
align-items: center;
flex-direction: row;
position: fixed;
top: 0px;
right: 0px;
left: auto;
width: auto;
height: 60px;
min-height: auto;
box-sizing: content-box;
margin: 0;
padding: 0;
list-style: none;
box-shadow: none;
overflow: hidden;
background-color: transparent;
z-index: 3;
margin: 0;
transition: all 0s ease;
}
.menu__item {
padding-left: 15px; padding-right: 15px;
padding-top: 0px; padding-bottom: 0px;
margin: 0px;
font-family: 'Open Sans Condensed', sans-serif;
font-size: 12pt;
font-weight: 700;
text-decoration: none;
transition-duration: .25s;
border-top: none;
padding-top: 30%; padding-bottom: 30%;
text-shadow: 2px 2px 6px rgba(0, 0, 0, 0.7);
}
.menu__social {font-size: 15pt; padding-left: 10px; padding-right: 10px; padding-top: 0px; padding-bottom: 0px; margin: 0px;}
.final__social {padding-right: 15pt}
.menu__social:hover, .menu__social:active {text-shadow: 1px 1px 1px rgba(255,255,255, .75);}
.menu__dropdownitem {
display: block;
padding-top: 5px;
padding-left: 11px;
padding-right: 11px;
padding-bottom: 5px;
color: #ffffff;
font-family: 'Open Sans Condensed', sans-serif;
font-size: 12pt;
font-weight: 700;
text-decoration: none;
transition-duration: .25s;
background-color: #000000;
}
.shows__dropbtn, .more__dropbtn {position: relative;}
.shows__dropdownhider, .more__dropdownhider {
position: fixed; top: 60px; background-color: #282828;
}
.menu__lastitem {border-bottom: none;}
.menu__overflowspacer {height: 0px}
.fa-caret-down {margin-left: 5px; font-size: 12pt; float: none;
padding: 0px; margin-right: 0px; margin-top: 0px; margin-bottom: 0px}
.menu__highlight {background-color: transparent;
font-family: 'Roboto', sans-serif; font-size: 9pt; font-weight: 500;
border: 1px solid #ffffff;
padding: 10px; margin-left: 10px; margin-right: 10px;
border-radius: 5px; box-shadow: 2px 2px 6px rgba(0,0,0,0.5);
text-shadow: none;}
.menu__highlight:hover, .menu__highlight:active {background-color: #ffffff; color: #000000;}
.menu__box :nth-child(6) {order: 1;}
}