Enhance Bootstrap 4 Navbar With Multilevel Dropdowns
File Size: | 3.3 KB |
---|---|
Views Total: | 5710 |
Last Update: | |
Publish Date: | |
Official Website: | Go to website |
License: | MIT |
A small jQuery script to enhance the Bootstrap 4 that allows you to insert unlimited levels of dropdown menus into the regular Bootstrap 4 navbar component.
Fully responsive and built with the standard Bootstrap 4 navbar & dropdown markup.
How to use it:
1. Add multilevel dropdown menus to the Bootstrap 4 navbar component.
<nav class="navbar navbar-expand-lg navbar-dark bg-dark"> <a class="navbar-brand" href="#">Navbar</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarNavDropdown"> <ul class="navbar-nav"> <li class="nav-item active"> <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a> </li> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> Dropdown link </a> <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink"> <li><a class="dropdown-item" href="#">Action</a></li> <li><a class="dropdown-item" href="#">Another action</a></li> <li class="dropdown-submenu"> <a class="dropdown-item dropdown-toggle" href="#">Submenu</a> <ul class="dropdown-menu"> <li><a class="dropdown-item" href="#">Submenu action</a></li> <li><a class="dropdown-item" href="#">Another submenu action</a></li> <li class="dropdown-submenu"> <a class="dropdown-item dropdown-toggle" href="#">Subsubmenu</a> <ul class="dropdown-menu"> <li><a class="dropdown-item" href="#">Subsubmenu action</a></li> <li><a class="dropdown-item" href="#">Another subsubmenu action</a></li> </ul> </li> <li class="dropdown-submenu"> <a class="dropdown-item dropdown-toggle" href="#">Second subsubmenu</a> <ul class="dropdown-menu"> <li><a class="dropdown-item" href="#">Subsubmenu action</a></li> <li><a class="dropdown-item" href="#">Another subsubmenu action</a></li> </ul> </li> </ul> </li> </ul> </li> </ul> </div> </nav>
2. The required CSS styles for the sub-menus.
.dropdown-submenu { position: relative; } .dropdown-submenu a::after { transform: rotate(-90deg); position: absolute; right: 6px; top: .8em; } .dropdown-submenu .dropdown-menu { top: 0; left: 100%; margin-left: .1rem; margin-right: .1rem; }
3. The necessary jQuery script to toggle sub-menus on click. Copy and place the following JavaScript snippets after jQuery library and done.
$('.dropdown-menu a.dropdown-toggle').on('click', function(e) { if (!$(this).next().hasClass('show')) { $(this).parents('.dropdown-menu').first().find('.show').removeClass('show'); } var $subMenu = $(this).next('.dropdown-menu'); $subMenu.toggleClass('show'); $(this).parents('li.nav-item.dropdown.show').on('hidden.bs.dropdown', function(e) { $('.dropdown-submenu .show').removeClass('show'); }); return false; });
This awesome jQuery plugin is developed by GalenCasstevens. For more Advanced Usages, please check the demo page or visit the official website.