Recently, I made the decision to switch the old code in my <head>
from jQuery to Vanilla JS.
Essentially, when you click on the hamburger icon, the mobile menu should fade-in and all the links should transition smoothly. I attempted to substitute the fadeToggle()
function by adding the class mob-menu-show
, along with a simple opacity transition, but it didn't work as expected. The same issue occurred with all the link translate
transitions that were previously functioning with jQuery. With jQuery, everything ran seamlessly each time I opened the mobile menu. It seems like there might be an issue with the JavaScript code that is somehow preventing the transitions from executing properly, although the class mobile-menu-animations
does activate since I can see the correct opacity change. Any assistance would be greatly appreciated, and I apologize for any messy code - I've just started delving into this.
HTML
<button class="hamburger hamburger--3dxy" type="button" aria-label="Menu" aria-controls="navigation">
<span class="hamburger-box">
<span class="hamburger-inner"></span>
</span>
</button>
<div id="nav-fullscreen" class="mobile-menu mobile-menu-controls">
<div id="mobile-menu-container">
<div id="mobile-menu-content">
<div id="logo-menu">
<div id="logo-mobile-menu"><a href="https://#"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 69.5 8.57" class="logo">...</svg> </a></div>
</div>
<div id="nav-items">
<div id="navigation">
<ul>
<li><a href="https://#">Item 1</a></li>
<li><a href="https://#">Item 2</a></li>
<li><a href="https://#">Item 3</a></li>
<li><a href="https://#">Item 4</a></li>
</ul>
</div>
</div>
<div id="menu-mobile-button-cont"><a id="button-mobile-menu" href="#">Buy Now</a></div>
</div>
</div>
</div>
CSS
.mobile-menu {
display: none;
opacity: 0;
transition: opacity 0.2s;
}
.mob-menu-show {
display: block !important;
opacity: 1;
transition: 0.2s opacity;
}
.mobile-menu-controls {
position: fixed;
top: 0;
left: 0;
width: 100%;
background-color: #fff;
z-index: 15;
}
#mobile-menu-container {
flex-direction: column;
display: flex;
width: 100%;
height: 100vh;
align-items: stretch;
top: 0;
text-align: left;
overflow-x: hidden;
overflow-y: scroll;
}
#mobile-menu-content {
width: 100%;
flex-direction: column;
display: flex;
align-items: stretch;
max-width: 1280px;
}
#nav-items {
flex-direction: unset;
margin-top: 7rem;
}
#menu-mobile-button-cont {
margin-left: 20px;
margin-bottom: 26px;
margin-right: 20px;
text-align: left;
flex-direction: unset;
display: flex;
width: 100%;
margin-top: 8rem;
}
/* Transitions */
#button-mobile-menu {
opacity: 0;
margin-left: 40px;
transform: translateX(0px);
transition: transform .2s,opacity .2s;
}
#logo-mobile-menu {
opacity: 0;
padding-left: 40px;
transform: translateX(0px);
transition: transform 0.2s, opacity 0.2s;
}
.mobile-menu-controls li {
opacity: 0;
padding-left: 40px;
transform: translateX(0px);
transition: transform 0.2s, opacity 0.2s;
}
#button-mobile-menu {
opacity: 0;
margin-left: 40px;
transform: translateX(0px);
transition: transform 0.2s, opacity 0.2s;
}
.mobile-menu-animations #logo-mobile-menu {
transform: translateX(-40px);
transition: transform 0.2s, opacity 0.2s;
transition-timing-function: ease, ease;
opacity: 1;
transition-delay: 0.1s;
}
.mobile-menu-animations li {
transform: translateX(-40px);
transition: transform 0.2s, opacity 0.2s;
transition-timing-function: ease, ease;
opacity: 1;
}
.mobile-menu-animations li:nth-child(1) {
transition-delay: 0.2s;
}
.mobile-menu-animations li:nth-child(2) {
transition-delay: 0.3s;
}
.mobile-menu-animations li:nth-child(3) {
transition-delay: 0.4s;
}
.mobile-menu-animations li:nth-child(4) {
transition-delay: 0.5s;
}
.mobile-menu-animations #button-mobile-menu {
transform: translateX(-40px);
transition: transform 0.2s, opacity 0.2s;
transition-timing-function: ease, ease;
opacity: 1;
transition-delay: 0.6s;
}
/* Prevent body scroll */
.prevent-scroll {
overflow: hidden;
}
Old jQuery Code
$(document).ready(function() {
$('.hamburger').click(function() {
$('.hamburger').toggleClass('is-active');
$('.mobile-menu').fadeToggle(350);
$('.mobile-menu-controls').toggleClass('mobile-menu-animations');
$('body').toggleClass("prevent-scroll");
});
});
Converted JS
document.addEventListener("DOMContentLoaded", function(event) {
let hamb = document.querySelector('.hamburger')
let mobmenu = document.querySelector('.mobile-menu')
let mobanim = document.querySelector('.mobile-menu-controls')
let bdy = document.querySelector('body')
hamb.addEventListener('click', function(e) {
this.classList.toggle('is-active');
mobmenu.classList.toggle('mob-menu-show');
mobanim.classList.toggle('mobile-menu-animations');
bdy.classList.toggle('prevent-scroll');
});
});