I am trying to make a responsive navigation on my WordPress site where I am building a template from scratch. I have decent experience with HTML and CSS(SCSS) some PHP but not so much Javascript or the WordPress way.
I am looking to remove the :hover element on my sub menu under the ‘services’ li and instead have it trigger on click on tablet and mobile devices. I understand it will be similar to how I have done the mobile menu button but I am struggling to figure out the best way to do it.
Can anyone give me an idea please? Thanks in advance.
function myFunction() {
var x = document.getElementById("myDIV");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
header {
height: 128px;
border-bottom: 1px solid #f0f0f0;
width: 100%;
position: fixed;
top: 0;
left: 0;
z-index: 4000;
background: white;
}
header .nav-container {
max-width: 100em;
margin: auto;
display: flex;
justify-content: space-between;
z-index: 45;
padding: 0 1.5rem;
}
header .nav-container .logo {
width: 14%;
padding-top: 2.8rem;
}
header .nav-container p {
display: none;
}
@media only screen and (max-width: 600px) {
header .nav-container p {
display: flex;
}
}
header .nav-container nav {
padding-top: 2rem;
}
@media only screen and (max-width: 600px) {
header .nav-container nav {
display: none;
}
}
@media only screen and (max-width: 600px) {
header .nav-container nav ul {
flex-direction: column;
display: flex;
}
}
header .nav-container nav ul li {
position: relative;
display: inline-block;
}
header .nav-container nav ul li a {
display: inline-block;
transition: all 0.5s linear;
text-decoration: none;
padding: 16px 10px;
color: #00458B;
}
header .nav-container nav ul li a:hover {
color: #00458B;
}
header .nav-container nav ul li ul {
display: none;
background: white;
position: absolute;
top: 100%;
width: 160px;
padding: 0;
z-index: 500;
}
header .nav-container nav ul li ul li, header .nav-container nav ul li ul a {
width: 100%;
}
header .nav-container nav ul li:hover ul {
display: block;
}
header .nav-container nav ul .menu-item-40 a {
padding: 0;
}
<header>
<div class="nav-container">
<p onclick="myFunction()"> Click</p>
<nav class="nav" role="navigation" id="myDIV">
<ul>
<li class="nav-item"><a href="/">Home</a>
</li>
<li class="nav-item"><a href="/">About us</a>
</li>
<li class="nav-item"><a href="/">Services</a>
<ul class="sub-menu">
<li class="nav-item "><a href="/">Windows</a>
</li>
<li class="nav-item"><a href="/">Glass</a>
</li>
<li class="nav-item"><a href="/">Doors</a>
</li>
<li class="nav-item"><a href="/">Roofline</a>
</li>
</ul>
</li>
<li class="nav-item"><a href="/">Our Work</a>
</li>
<li class="nav-item"><a href="/">Contact Us</a>
</li>
</ul>
</nav>
</div>
</header>
2
Answers
Wrap it with Media Query so it doesn’t work on Mobile and tablet.
This is only one of many possible solutions, but I think it gives you an idea off how to solve the problem.
First you have to wrap following selector with a media query to disable the hover when your mobile button shows up. In your case it would look like this:
To attach the toggle functionality I would suggest to add a
js-submenu-toggle
class to alla
elements which have a submenu as sibling. I prefer to add ajs
prefix to my classes to mark them as classes that are only used in combination with javascript and have no styling attached to them:For the actual functionality use the
toggle
function to add and remove anis-active
class on click to thesubmenu
element and thematchMedia
function to make the toggle functionality only available when your mobile menu button is visible:The
is-active
class should look like this: