Check out my custom jQuery
menu implementation below, also available on JSfiddle here:
$(document).ready(function () {
$(".main_menu_01, .main_menu_02").on('click', function () {
var $panel = $(this).next('.panel');
if ($panel.is(':visible')) {
$panel.add($panel.find('.panel')).slideUp(500);
$(this).hasClass('main_menu_01') ? $('.menu').removeClass('active') : $(this).removeClass('active');
} else {
$panel.slideDown(500);
$(this).addClass('active');
}
});
});
.panel{
width: 100%;
padding-left: 0%;
font-weight: bold;
overflow: hidden;
display:none;
}
.main_menu_01 {
padding-left: 1%;
background: blue;
}
.main_menu_02 {
padding-left: 5%;
background: lime;
}
.sub_menu_01{
padding-left: 1%;
background: lime;
}
.sub_menu_02{
padding-left: 10%;
background: lime;
}
.main_menu_01:before, .main_menu_02:before {
content:'+';
width:20px;
display:inline-block;
}
.main_menu_01.active:before, .main_menu_02.active:before {
content:'-';
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="menu main_menu_01"> 1.0 Main Menu </li>
<ul class="panel">
<li class="sub_menu_01"> 1.1 Sub Menu </li>
<li class="menu main_menu_02"> 1.2 Sub Menu </li>
<ul class="panel">
<li class="sub_menu_02"> <a> 1.2.1 Sub Menu </a> </li>
<li class="sub_menu_02"> <a> 1.2.2 Sub Menu </a> </li>
</ul>
</ul>
<li class="menu main_menu_01"> 2.0 Main Menu </li>
<ul class="panel">
<li class="sub_menu_01"> 2.1 Sub Menu </li>
<li class="menu main_menu_02"> 2.2 Sub Menu </li>
<ul class="panel">
<li class="sub_menu_02"> <a> 2.2.1 Sub Menu </a> </li>
<li class="sub_menu_02"> <a> 2.2.2 Sub Menu </a> </li>
</ul>
</ul>
I've encountered an issue with the functionality where toggling between menus changes signs from + to a -, as intended. However, when closing 1.0 Main Menu
, both the submenu and the main menu signs change back to + instead of staying as -.
This inconsistency arises due to the code line $(this).hasClass('main_menu_01')
affecting all classes with the name main_menu_01
.
I'm currently facing difficulty in resolving this issue. Any suggestions on altering the code to achieve the desired behavior?