我有一个与此类似的Bootstrap 4布局: https://www.codeply.com/go/yLO99L66MD
如果导航项太多,我希望隐藏它们,所以我添加了这个:nav {overflow:hidden}
。这样做的工作,但问题是它也隐藏了我的下拉菜单。如何隐藏额外的菜单项,但仍然允许显示可见项目的下拉菜单?
<nav class="navbar navbar-expand-md navbar-dark bg-dark">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav mr-auto">
<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="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="http://example.com" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
<div class="dropdown-menu" aria-labelledby="dropdown01">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="#">aaaaaaaa aaaaa </a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">bbbbbb bbbbb</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">cccccccc cccccccc </a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">dddd ddddddddd </a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">eeeeeeeeeeeeeeeeee cv kbc vxckjvhkxcv </a>
</li>
</ul>
</div>
</nav>
<div class="container">
<div class="starter-template">
<h1>Bootstrap starter template</h1>
<p class="lead">Use this document as a way to quickly start any new project.
<br> All you get is this text and a mostly barebones HTML document.</p>
</div>
</div>
<!-- /.container -->
答案 0 :(得分:12)
您可以使用导航栏作为给定示例。
当您调整浏览器大小时,如果没有足够的空间,导航项将被移动到下拉列表中。
<强> Demo 强>
[1]: http://jsfiddle.net/swasatz/3fn4d5oq/
代码段演示
以完整页面模式打开代码段并调整浏览器大小以查看更改。
$(document).ready(function () {
var menu = $("#nav-bar-filter"),
subMenu = $(".subfilter"),
more = $("#more-nav"),
parent = $(".filter-wrapper"),
ww = $(window).width(),
smw = more.outerWidth();
menu.children("li").each(function () {
var w = $(this).outerWidth();
if (w > smw) smw = w + 20;
return smw
});
more.css('width', smw);
function contract() {
var w = 0,
outerWidth = parent.width() - smw - 50;
for (i = 0; i < menu.children("li").size(); i++) {
w += menu.children("li").eq(i).outerWidth();
if (w > outerWidth) {
menu.children("li").eq(i - 1).nextAll()
.detach()
.css('opacity', 0)
.prependTo(".subfilter")
.stop().animate({
'opacity': 1
}, 300);
break;
}
}
}
function expand() {
var w = 0,
outerWidth = parent.width() - smw - 20;
menu.children("li").each(function () {
w += $(this).outerWidth();
return w;
});
for (i = 0; i < subMenu.children("li").size(); i++) {
w += subMenu.children("li").eq(i).outerWidth();
if (w > outerWidth) {
var a = 0;
while (a < i) {
subMenu.children("li").eq(a)
.css('opacity', 0)
.detach()
.appendTo("#nav-bar-filter")
.stop().animate({
'opacity': 1
}, 300);
a++;
}
break;
}
}
}
contract();
$(window).on("resize", function (e) {
($(window).width() > ww) ? expand() : contract();
ww = $(window).width();
});
});
body {
font-family: verdana;
min-width: 250px;
}
ul#more-nav, ul#nav-bar-filter {
display: inline-block;
vertical-align: top;
}
ul {
list-style-type: none;
padding: 0;
margin: 0;
}
li {
padding: 4px 8px 4px 8px;
margin: 0;
}
#nav-bar-filter li {
display: inline-block;
font-weight: bold;
}
a {
text-decoration: none;
color: #666;
font-size: 13px;
}
.filter-wrapper {
width: 100%;
background: #eee;
padding: 5px 10px 5px 10px;
}
#more-nav {
float: right;
}
.subfilter{
padding-top: 10px;
}
.subfilter li {
margin: 0 0 0 20px;
padding: 5px 0 0 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="twelve columns filter-wrapper">
<ul class="nav-bar-filter" id="nav-bar-filter">
<li><a href="#">All</a>
</li>
<li><a href="#">Small</a>
</li>
<li><a href="#">Medium</a>
</li>
<li><a href="#">Extra large</a>
</li>
<li><a href="#">Text</a>
</li>
<li><a href="#">Small-1</a>
</li>
<li><a href="#">Medium-1</a>
</li>
<li><a href="#">Extra large text</a>
</li>
<li><a href="#">Large text</a>
</li>
<li><a href="#">Another text</a>
</li>
<li><a href="#">text</a>
</li>
</ul>
<ul id="more-nav">
<li><b><a href="#">More ></a></b>
<ul class="subfilter"></ul>
</li>
</ul>
</div>
答案 1 :(得分:1)
对于您希望在移动设备上以隐藏方式查看的菜单项,请在您想要隐藏的列表项周围包围一个范围,并添加bootstrap 4 beta 隐藏值,就像这样。
<span class="d-none d-xs-block">
<li class="nav-item">
<a class="nav-link" href="#">aaaaaaaa 1234 </a>
</li>
</span>
您还可以将该类应用于<a>
标记,以隐藏下拉菜单中的特定链接。此外,修改d-xs-block
类以符合您的规格。
我想向您提供一些资料,以帮助指导您了解特定断点处的隐藏元素。
答案 2 :(得分:0)
(更新的答案)您可以使用jQuery显示适合窗口的导航菜单并隐藏其余内容:
<强> jQuery的:强>
$(window).resize(function() {
var winwidth=$(window).width(),totwidth=$('.navbar .navbar-brand').outerWidth(),shownnum=0;
var navs=$('.navbar .nav-item');
navs.each(function(i) {
totwidth+=$(this).outerWidth();
if(totwidth<winwidth) shownnum=i+1; else return false;
});
navs.show().slice(shownnum).hide();
});
<强> CSS:强>
.navbar {overflow:hidden}
.nav-item {white-space:nowrap}
答案 3 :(得分:0)
以下片段实现了一个&#34; mega菜单&#34; (类似于Yamm3 Megamenu for Bootstrap 3,参见http://geedmo.github.io/yamm3/)基于Bootstrap 4的flexbox类:
<!-- adapted from bs-repo basic navbar example code -->
<nav id="navbar" 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="#navbarSupportedContent">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<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="#">Other</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="..."
id="navbarDropdownMenuLink" data-toggle="dropdown">
Dropdown link
</a>
<div class="dropdown-menu">
<ul class="d-md-inline-flex flex-md-row">
<li class="dropdown-item">
<ul class="d-md-inline-flex flex-md-column">
<li class="dropdown-item"><a href="#">Action</a></li>
<li class="dropdown-item"><a href="#">Another</a></li>
<li class="dropdown-item"><a href="#">Another</a></li>
</ul>
</li>
<li class="dropdown-item">
<ul class="d-md-inline-flex flex-md-column">
<li class="dropdown-item"><a href="#">Action</a></li>
<li class="dropdown-item"><a href="#">Another</a></li>
<li class="dropdown-item"><a href="#">Another</a></li>
</ul>
</li>
<li class="dropdown-item">
<ul class="d-md-inline-flex flex-md-column">
<li class="dropdown-item"><a href="#">Action</a></li>
<li class="dropdown-item"><a href="#">Another</a></li>
<li class="dropdown-item"><a href="#">Another</a></li>
</ul>
</li>
</ul>
</div>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="text"
placeholder="Search">
<button class="btn btn-outline-success my-2 my-sm-0"
type="submit">Search</button>
</form>
</div>
</nav>
与Bootstrap的基本导航栏示例相比,自div
以来dropdown-menu
包含构成菜单的ul
display: flex
的另一个display: none
与Bootstrap的下拉支持脚本(popper.js)动态设置的md
不一样。
此变体在md
分辨率及更高版本上应用弹性行布局,同时对以下分辨率应用简单的无序列表格式 - lengthr = 0
。
答案 4 :(得分:0)
这是Bootstrap 4的另一个选项,它将多余的Navbar项折叠到右侧的下拉菜单中。使用一些逻辑(这是使用jQuery)来控制菜单中项目的放置...
function (menu,maxHeight) {
var nav = $(menu);
// check height of menu
var navHeight = nav.innerHeight();
// when the height is taller the navbar has wrapped onto 2 lines
if (navHeight >= maxHeight) {
$(menu + ' .dropdown').removeClass('d-none');
$(".navbar-nav").removeClass('w-auto').addClass("w-100");
while (navHeight > maxHeight) {
// add child to dropdown
var children = nav.children(menu + ' li:not(:last-child)');
var count = children.length;
$(children[count - 1]).prependTo(menu + ' .dropdown-menu');
navHeight = nav.innerHeight();
}
$(".navbar-nav").addClass("w-auto").removeClass('w-100');
}
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) {
// remove child from dropdown
collapsed = $(menu + ' .dropdown-menu').children('li');
$(collapsed[0]).insertBefore(nav.children(menu + ' li:last-child'));
navHeight = nav.innerHeight();
}
if (navHeight > maxHeight) {
autocollapse(menu,maxHeight);
}
}
}
答案 5 :(得分:-1)
隐藏菜单后增加div或break菜单的宽度。
答案 6 :(得分:-1)
这是一个自然的Bootstrap 4解决方案,不需要JS或额外的CSS。您可以根据文档使用 div ,而不是使用 ul ,并使用display属性显示或隐藏某些元素导航栏,包括根据屏幕尺寸的下拉列表。
以下是文档说的内容:
因为我们使用导航类,所以如果您愿意,可以完全避免使用基于列表的方法。 nav documentation
这是一个例子,我也将下拉列为div:
<div class="navbar-nav ml-auto">
<a class="nav-item nav-link" href="#">How does it work</a>
<a class="nav-item nav-link" href="#">Demo</a>
<div class="nav-item dropdown" href="#">
<a class="nav-link dropdown-toggle" href="#" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
<div class="dropdown-menu" aria-labelledby="dropdown01">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</div>
<a class="nav-item nav-link d-block d-lg-none d-xl-none" href="#">Show only on md and down</a>
<a class="nav-item nav-link d-none d-lg-block d-md-block" href="#">Show on md and lg</a>
</div>
它是一种有效的原生解决方案。您可以在任何屏幕尺寸上以这种方式显示/隐藏任何导航元素,折叠后它也会显示在移动菜单中。
P.S。不需要溢出:隐藏
祝你好运!