I have set up a test page using Bootstrap 4.3.1 to create a menu bar with submenus. The menu should display a triangle pointer when hovered over, indicating the selection. The user should be able to move the mouse to the submenu bar to choose a submenu. When clicking on a menu with submenus, the submenu bar (along with the triangle pointer) should remain in place until a submenu or another top menu is selected. However, I am encountering an issue where the styles of the submenu are being altered when trying to maintain the hover selection by adding/removing classes. Can someone offer assistance with this problem?
Thanks in advance.
Below are two screenshots:
https://i.sstatic.net/HfmWt.png https://i.sstatic.net/pkriL.png Html:
<nav class="navbar navbar-expand-md navbar-dark ">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#menubar">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse" id="menubar">
<ul class="nav navbar-nav">
<li class="nav-item ">
<a class="nav-link" href="#">Menu1</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link" data-toggle="dropdown" href="#">Menu2</a>
<ul class="dropdown-menu" role="menu">
<li><a href="#">Submenu1</a></li>
<li><a href="#">Submenu2</a></li>
<li><a href="#">Submenu3</a></li>
</ul>
</li>
</ul>
</div>
</nav>
css:
@media (max-width: 767px) {
.navbar-collapse {
background-color: #565149;
}
}
#menubar ul {
height: 44px;
padding-top: 5px;
}
#menubar .nav-link {
font-size: 14px;
padding: 8px 50px 0 0 !important;
color: white !important;
}
#menubar .nav-link:focus,
#menubar .nav-link:hover,
#menubar .nav-link:visited {
color: white !important;
}
#menubar .navbar{
padding-left: 0 !important;
}
.navbar.navbar-dark,
.navbar.navbar-light {
height: 44px;
margin: 0 15%;
}
nav.navbar .navbar-nav li.nav-item.active:after{
content: "";
position: relative;
margin-left: -31px;
left: 50%;
bottom: 15px;
border-left: 6px solid transparent;
border-right: 6px solid transparent;
border-bottom: 6px solid white;
}
/*submenu*/
#menubar li.dropdown.show {
position: static;
}
#menubar li.dropdown.show .dropdown-menu {
display: table;
width: 100%;
text-align: center;
left: 0;
right: 0;
margin: 0;
}
.dropdown-menu > li {
display: table-cell;
padding-top: 6px;
}
.dropdown-menu > li a{
font-weight:600;
}
.dropdown-menu > li a:hover {
text-decoration: none;
}
js:
$("#menubar li.dropdown>a.nav-link").hover(function () { $(this).closest("li").addClass("active show") },
function () { $(this).closest("li").removeClass("active show") });