gpt4 book ai didi

jquery - Bootstrap 4 灵活响应式导航栏菜单

转载 作者:太空宇宙 更新时间:2023-11-04 06:53:53 24 4
gpt4 key购买 nike

请注意,我不是在问如何对齐导航菜单中的元素(左对齐或右对齐),而是如何将元素从导航栏动态移动到下拉列表。

我在我的元素中使用 Bootstrap 4.1。我的顶部主菜单包含许多链接。最后一项是包含附加链接的下拉列表: Bootstrap navbar

我想在调整窗口大小时,将标题中没有足够空间的正确元素移到下拉元素中。

我想要的: Bootstrap wanted functionality

但是在调整大小时,元素正在缩小。然后,对于“md”(和更小的)断点,默认的导航栏功能被激活: Bootstrap default functionality Bootstrap mobile navbar

使用 flexbox 的解决方案也是可以接受的。使用默认 Bootstrap 导航栏链接到简单演示: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>

谢谢!

编辑:

这是我当前正在处理的窗口的调整大小功能:

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;

上面的链接也已更新。

它在缩小屏幕时有效,但我需要实现:

  • 增加屏幕宽度时;
  • 在“md”(或更小的)断点中,使用默认 Bootstrap 的功能

最佳答案

而是使用高度来检测导航栏元素何时换行。您可能需要调整 JS 以适应其他菜单项(关于、帮助、注销)。这是 Bootstrap 4 的 jQuery 函数...

var autocollapse = function (menu,maxHeight) {

var nav = $(menu);
var navHeight = nav.innerHeight();
if (navHeight >= maxHeight) {
$(menu + ' .dropdown').removeClass('d-none');
while (navHeight > maxHeight) {
var children = nav.children(menu + ' li:not(:last-child)');
var count = children.length;
$(children[count - 1]).prependTo(menu + ' .dropdown-menu');
navHeight = nav.innerHeight();
}
}
else {
var collapsed = $(menu + ' .dropdown-menu').children(menu + ' li');

if (collapsed.length===0) {
$(menu + ' .dropdown').addClass('d-none');
}

while (navHeight < maxHeight && (nav.children(menu + ' li').length > 0) && collapsed.length > 0) {
collapsed = $(menu + ' .dropdown-menu').children('li');
$(collapsed[0]).insertBefore(nav.children(menu + ' li:last-child'));
navHeight = nav.innerHeight();
}

if (navHeight > maxHeight) {
autocollapse(menu,maxHeight);
}
}
};


$(document).ready(function () {

// when the page laods
autocollapse('#nav',50);

// when the window is resized
$(window).on('resize', function () {
autocollapse('#nav',50);
});

});

演示:https://www.codeply.com/go/wKWHgsMXah


相关:Bootstrap navbar hide menu elements when resizing

关于jquery - Bootstrap 4 灵活响应式导航栏菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52607027/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com