NOTE that I'm not asking how to align items in navbr menu (left or right), but how to dynamically move items from navbar to dropdown.
I'm using Bootstrap 4.1 in my project. My top main menu contains many links. The last item is a dropdown which contains additional links:
I want, when resizing the window, the right items for which doesn't have enough space in the header, to be moved into the dropdown item.
But when resizing, the items are shrinking. Then, for "md" (and smaller) breakpoints, the default navbar functionality is activated:
Solutions with flexbox are acceptable, too. Link to simple demo with default Bootstrap navbar: CodePen
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<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">
<a class="nav-link" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Menu item 4</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Menu item 5</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Menu item 6</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Menu item 7</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Menu item 8</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown Menu
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="#">Menu item 9</a>
<a class="dropdown-item" href="#">Menu item 10</a>
<a class="dropdown-item" href="#">About</a>
<a class="dropdown-item" href="#">Help</a>
<a class="dropdown-item" href="#">Sign out</a>
</div>
</li>
</ul>
</div>
</nav>
Thank you!
EDIT:
This is the window's resize function on which I'm currently working:
let addMenu = $("#navbarNavDropdown > ul >li.dropdown"),
addMenuList = addMenu.children("ul"),
menuItems = $("#navbarNavDropdown > ul > li:visible:not('.dropdown')");
function _resize() {
var itemsWidth = 0,
startWrap = false,
navWidth = $("#navbarNavDropdown").width() - addMenu.width();
menuItems.each(function () {
if (startWrap == false) {
if (itemsWidth + $(this).width() < navWidth) {
itemsWidth += $(this).width();
}
else {
startWrap = true;
addMenuList.prepend(this);
}
}
else {
addMenuList.prepend(this);
}
});
}
window.onresize = _resize;
Link above has been updated, too.
It works when making the screen smaller, but I need to implement:
- when increasing the screen's width;
- in "md" (or smaller) breakpoint, to use the default bootstrap's functionality