Greetings,
I am currently learning on the job as I work on a new website for my family business.
I am struggling to make the drop-down navigation menu function properly on both desktop and mobile. I want it to have a simple design similar to the main navigation, and I have tried different approaches to make it work like the toggle button. However, it is not behaving as expected.
My goal is to have the drop-down links display on hover for desktop users and require a touch for mobile users.
HTML
<div class="navigation-bar">
<div class="navigation-item">
<div class="logo">
<a href="index.html">
<img src="/assets/img/logo.jpg" alt="Company Logo">
</a>
</div>
<ul class="navigation-bar-menu">
<li class="active"><a href="index.html" class="active">Home</a></li>
<li><a href="#">Services <i class="fa-thin fa-chevron-down"></i></a>
<ul class="navigation-bar-dropdown">
<li><a href="#">Managed Print Service</a></li>
<li><a href="#">Document Management</a></li>
<li><a href="#">Machine Finance Options</a></li>
<li><a href="#">Machine Repair & Support</a></li>
</ul>
</li>
<li><a href="#">Products <i class="fa-thin fa-chevron-down"></i></a>
<ul class="navigation-bar-dropdown">
<li><a href="#">Machines</a></li>
<li><a href="#">Ricoh</a></li>
<li><a href="#">Lexmark</a></li>
<li><a href="#">Konica Minolta</a></li>
<li><a href="#">Software</a></li>
<li><a href="#">Kofax</a></li>
<li><a href="#">Papercut</a></li>
<li><a href="#">Filestar</a></li>
</ul>
</li>
<li><a href="contact.html">Contact Us</a></li>
</ul>
<button class="navigation-bar-toggle">
<i class="fa-light fa-bars"></i>
Menu
</button>
</div>
</div>
CSS
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
background-color: #242424;
font-family: 'proxima-nova', sans-serif;
font-weight: 100;
font-style: normal;
font-size: 16px;
line-height: 1.5rem;
}
li {
list-style-type: none;
}
a {
text-decoration: none;
color: inherit;
}
.navigation-bar {
background-color: #ffffff;
padding: 2rem 1rem;
position: relative;
box-shadow: rgba(14, 30, 37, 0.12) 0px 2px 4px 0px, rgba(14, 30, 37, 0.32) 0px 2px 16px 0px;
}
.navigation-item {
display: flex;
justify-content: space-between;
align-items: center;
}
.logo img {
width: 10rem;
display: block;
}
.navigation-bar-toggle {
padding: 1rem;
background-color: #ecf0f1;
color: inherit;
cursor: pointer;
text-transform: uppercase;
border: none;
font: inherit;
border-radius: 0.25rem;
}
.navigation-bar-dropdown {
display: flex;
flex-direction: column;
}
.
.
.
<p>JavaScript</p>
<pre><code> window.addEventListener('DOMContentLoaded', () => {
const menuToggler = document.querySelector('.navigation-bar-toggle')
const menu = document.querySelector('.navigation-bar-menu')
menuToggler.addEventListener('click', () => {
menu.classList.toggle('open')
})
})
If you have any suggestions or solutions, I would greatly appreciate your assistance. https://jsfiddle.net/fc471p3e/4/