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

标签 javascript jquery html css twitter-bootstrap

我想做的是隐藏 <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/

相关文章:

javascript - 在不显示模式下克隆一个 div

javascript - 我无法从我的复选框中获取值,我需要将提交按钮下的复选框上的值打印到 div 中

javascript - HTML 表单上传实际文件而不是快捷方式

javascript - 如何同步确定 JavaScript Promise 的状态?

jquery - 插入图像时防止跳转文本

javascript - 点击后下拉框消失

javascript - 如果设置了变量,文本跨度的简写?

javascript - 更改 HTML 选项选择作为图标样式

javascript - 添加 data-ajax ="false"后退按钮不起作用

javascript - 在 Shopware 6 中覆盖现有的插件 JS