When adding the option to open only one sub-menu at a time, it generates the error that only one sub-menu should always be open and I do not understand the reason, I attach a test image.
enter image description here
enter image description here
As you can see when one clicks it displays the menu but if I click it again it does not close but it opens again, in the image you see the closed arrow "↟" an up arrow and when the sub-menu opens the arrow changes down "↡", I say this to show the error that the menu never closes.
This is the html code
`
<nav class='animated flipInX'>
<ul>
<li>
<a href='#'>
<div class='fa fa-home'></div>
</a>
</li>
<li>
<a href='#'>
About
</a>
</li>
<li class='sub-menu'>
<a href='#'>
Products
<i class='fa fa-angle-down'></i>
</a>
<ul>
<li>
<a href='#'>
Product Item
</a>
</li>
<li>
<a href='#'>
Product Item
</a>
</li>
<li>
<a href='#'>
Product Item
</a>
</li>
</ul>
</li>
<li class='sub-menu'>
<a href='#'>
Services
<i class='fa fa-angle-down'></i>
</a>
<ul>
<li>
<a href='#'>
Product Item
</a>
</li>
<li>
<a href='#'>
Product Item
</a>
</li>
<li>
<a href='#'>
Product Item
</a>
</li>
</ul>
</li>
<li>
<a href='#'>
Contact Us
</a>
</li>
</ul>
</nav>
This is the js code
$(".sub-menu ul").hide()
$(".sub-menu a").click(function () {
$(".sub-menu ul").not($(this)).hide();
$(this).parent(".sub-menu").children("ul").slideToggle("200");
$(this).find("i.fa").toggleClass("fa-angle-up fa-angle-down");
});
`
I ask for help to solve this problem, the idea is to let you close the normal menu but do not change the functionality of not being able to open more than one menu at the same time.
I tried many jquery options but I don’t understand the error yet.
2
Answers
Here you go,
A simple solution, with one
stack
keeping track of the open items. Easy to change the number of simultaneously allowed open sub-menus. The only addition is theopen
class (and the correspondingCSS
).