gpt4 book ai didi

javascript - 使用 jquery 操作 Bootstrap 导航栏元素

转载 作者:行者123 更新时间:2023-11-28 03:11:36 26 4
gpt4 key购买 nike

我想做的是隐藏 <li>从导航栏到调整大小的下拉菜单中的元素。

enter image description here

我试过了,但我以这个有问题的菜单结束了。 http://i.imgur.com/1uJ7hbG.gif

这是js:

$().ready(function () {
//we reconstruct menu on window.resize
$(window).on("resize", function (e) {
var parentWidth = $("#normal").parent().width() - 40;
var ulWidth = $(".menutohide").outerWidth();
var menuLi = $("#normal > li");
var liForMoving = new Array();
//take all elements that can't fit parent width to array
menuLi.each(function () {
ulWidth += $(this).outerWidth();
if (ulWidth > parentWidth) {
console.log(ulWidth);
liForMoving.push($(this));
}
});
if (liForMoving.length > 0) { //if have any in array -> move em to "more" ul
e.preventDefault();
liForMoving.forEach(function (item) {
item.clone().appendTo(".links-hidden");
item.remove();
});
}
else if (ulWidth < parentWidth) { //check if we can put some 'li' back to menu
liForMoving = new Array();
var moved = $(".links-hidden > li");
for (var i = moved.length - 1; i >= 0; i--) { //reverse order
var tmpLi = $(moved[i]).clone();
tmpLi.appendTo($("#normal"));
ulWidth += $(moved[i]).outerWidth();
if (ulWidth < parentWidth) {
$(moved[i]).remove();
}
else {
ulWidth -= $(moved[i]).outerWidth();
tmpLi.remove();
}
}
}
if ($(".links-hidden > li").length > 0) { //if we have elements in extended menu - show it
$(".menutohide").show();
}
else {
$(".menutohide").hide();
}
});

$(window).trigger("resize"); //call resize handler to build menu right
});

这是 html:

<nav class="navbar navbar-default" role="navigation">
<ul class="nav navbar-nav" id="normal">
<li><a href="#" class="first-child">Home</a></li>
<li><a href="#">News</a></li>
<li><a href="#">Download</a></li>
<li><a href="#">Twitch Live</a></li>
<li class="disabled"><a href="#">Ranking</a></li>
</ul>
<ul class="nav navbar-nav menutohide">
<li class="dropdown hidden-dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="true">
<i class="fa fa-bars fa-fw"></i>
</a>
<ul class="dropdown-menu links-hidden" role="menu">
<!--Hidden links here-->
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
<h6 class="hidden-xs">Bambini</h6>
<img src="http://placehold.it/250x250/000000"/>
<span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><a href="#">Account info</a></li>
<li><a href="#">Logoff</a></li>
</ul>
</li>
</ul>
</nav>

最佳答案

我认为您可以使用 CSS 实现此目的 @media询问。只要给任何你想隐藏的某种id,然后你就可以使用display: none隐藏它。

@media (max-width: 767px) {
#contact-nav-link {
display: none;
}
}

这是一个 JSFiddle - <li>显示“联系人”的元素将显示在导航栏中,但当调整窗口大小时(并且导航栏折叠),它会从下拉列表中消失。

关于javascript - 使用 jquery 操作 Bootstrap 导航栏元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30274363/

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