I am trying to create a menu button that when hovered, reveals a drop down set of links on desktop. However, obviously users can't hover on mobiles etc. so have been trying to work out how to make the button reveal the menu on click too.
I have so far managed to come up with adding and removing the .show-nav
class on mouse enter and leave using jquery. I tried adding the following code block but it obviously also affects desktop, which makes the menu kinda screwy if someone clicks the button (as the class is applied on the hover and then added and removed permanently using the click).
Thanks to cesare everything is working as it should, apart from in Chrome on iOS. djtwigg's solution is working in Chrome but not safari. Is it possible to merge the two solutions together?
<nav>
<ul>
<li id="nav-button">
<a id="nav-click" href="#">Menu <i class="fa fa-bars"></i></a>
<ul class="sub-nav">
<li><a href="#">Link1</a></li>
<li><a href="#">Link2</a></li>
<li><a href="#">Link3</a></li>
<li><a href="#">Link4</a></li>
</ul>
</li>
</ul>
</nav>
var flag = false;
container.bind('touchstart', function(){
if (!flag) {
flag = true;
setTimeout(function(){ flag = false; }, 260);
list.toggleClass('show-nav');
}
return false
});
container.hover(function(){
list.addClass('show-nav');
}, function(){
list.removeClass('show-nav');
});
});