简体   繁体   English

Bootstrap导航栏崩溃问题

[英]Bootstrap navigation bar collapsing issue

I have written a navigation bar based on the example given in the components link in Bootstrap. 我已经基于Bootstrap 组件链接中给出的示例编写了一个导航栏。 But the navigation bar does not collapse in a right way as it is supposed to do. 但是,导航栏没有以应有的方式正确折叠。 As shown in the process below: 如下图所示:

State 1 状态1 在此处输入图片说明

State 2 状态2 在此处输入图片说明

State 3 状态3 在此处输入图片说明

the navigation bar is not collapsed sharply, rather, it will become 2 columns first and then collapse. 导航栏不会急剧折叠,而是先变为2列,然后折叠。

I have found a same problem that has been asked previously in stackoverflow . 我发现了一个以前在stackoverflow中提出过的问题。 It seems like the problem is caused by the default breaking point defined in bootstrap. 看来问题是由引导程序中定义的默认断点引起的。 However, I do not know where to change the breaking point and how can I do it. 但是,我不知道在哪里更改断点以及如何更改。

I am using the latest bootstrap version, and following is the snippet of my navigation bar code. 我正在使用最新的引导程序版本,以下是我的导航条形码的摘要。

Thanks in advance for any help and advice! 预先感谢您的任何帮助和建议!

<!--navigation bar-->
    <nav class="navbar navbar-inverse navbar-fixed-top">

        <div class="container">

            <div class="navbar-header"><!--brand and toggle get grouped for better mobile display-->

                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navItems">

                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>

                </button>

                <a class="navbar-brand" href="#">Logo</a><!--end of logo-->

            </div><!--end of logo & collapsing logic-->

            <div class="collapse navbar-collapse" id="navItems">

                <ul class="nav navbar-nav">

                    <li><a href="#main" class="scroll">HOME</a></li>

                    <li><a href="#about" class="scroll">ABOUT</a></li>

                    <li><a href="#work" class="scroll">WORK</a></li>

                    <li><a href="#blog" class="scroll">BLOG</a></li>

                    <li><a href="#contact" class="scroll">CONTACT</a></li>

                </ul><!--end of menu list-->

                <form class="navbar-form navbar-left">

                    <input type="text" class="form-control" placeholder="Search on this site" id="searchInput ">

                    <button type="submit" class="btn btn-default"><span class="glyphicon glyphicon-search"></button>

                </form><!--end of search bar-->

                <ul class="nav navbar-nav navbar-right">

                    <li class="dropdown">

                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><span class="glyphicon glyphicon-user"></span> My Account <span class="caret"></span></a>

                        <ul class="dropdown-menu" role="menu">

                            <li><a href="#"><span class="glyphicon glyphicon-wrench"></span> Settings</a></li>

                            <li><a href="#"><span class="glyphicon glyphicon-refresh"></span> Update Profile</a></li>

                            <li class="divider"></li>

                            <li><a href="#"><span class="glyphicon glyphicon-off"></span> Sign Out</a></li>

                        </ul><!--end of sub menu-->

                    </li><!--end of dropdown menu-->

                </ul><!end of the user account menu-->

            </div><!--end of collpased nav-->

        </div><!--end of container-->

    </nav><!--end of navbar-->

Update 更新

I have looked into the solution provide by this link , which does not work in the newest version of Bootstrap. 我已经研究了此链接提供的解决方案,该解决方案在最新版本的Bootstrap中不起作用。

And I have locate the code block which defines the breakpoint of the collapsing effect: 我找到了定义崩溃效果断点的代码块:

@media (min-width: 768px) {
  .navbar-collapse {
    width: auto;
    border-top: 0;
    -webkit-box-shadow: none;
            box-shadow: none;
  }
  .navbar-collapse.collapse {
    display: block !important;
    height: auto !important;
    padding-bottom: 0;
    overflow: visible !important;
    visibility: visible !important;
  }
  .navbar-collapse.in {
    overflow-y: visible;
  }
  .navbar-fixed-top .navbar-collapse,
  .navbar-static-top .navbar-collapse,
  .navbar-fixed-bottom .navbar-collapse {
    padding-right: 0;
    padding-left: 0;
  }
}

And I tried to modify the defined min-width, however I changed, it just does not work. 而且我尝试修改定义的最小宽度,但是我更改了,但是它不起作用。

I finally figured it out: 我终于想通了:

To do this in the latest version: 要在最新版本中执行此操作:

I have modified three parts in the css file: 我已经修改了css文件中的三个部分:

First you need to set when the navbar-header will float to left (in may case 1000px): 首先,您需要设置导航栏标题何时向左浮动(可能为1000像素):

@media (min-width: 1000px) {
  .navbar-header {
    float: left;
  }
}

Then you will need to decide when all the menu button will be disappeared: 然后,您需要确定何时所有菜单按钮都将消失:

@media (min-width: 1000px) {
  .navbar-collapse {
    width: auto;
    border-top: 0;
    -webkit-box-shadow: none;
            box-shadow: none;
  }
  .navbar-collapse.collapse {
    display: block !important;
    height: auto !important;
    padding-bottom: 0;
    overflow: visible !important;
    visibility: visible !important;
  }
  .navbar-collapse.in {
    overflow-y: visible;
  }
  .navbar-fixed-top .navbar-collapse,
  .navbar-static-top .navbar-collapse,
  .navbar-fixed-bottom .navbar-collapse {
    padding-right: 0;
    padding-left: 0;
  }
}

Last you need to define when the navbar-toggle will take effect: 最后,您需要定义navbar-toggle何时生效:

@media (min-width: 1000px) {
  .navbar-toggle {
    display: none;
  }
} 

Then just wait to see the magic happen: 然后,等待魔术发生:

When the screen reached the 1000px threshold: 当屏幕达到1000px阈值时:

From

在此处输入图片说明

To

在此处输入图片说明

Seems like you should be able to override the default navbar collapse point like this.. (Assuming 1000 pixels in the new collapse point): 似乎您应该能够像这样覆盖默认的导航栏折叠点。(假设在新的折叠点中有1000像素):

@media (max-width: 1000px) {
    .navbar-header {
        float: none;
    }
    .navbar-toggle {
        display: block;
    }
    .navbar-collapse {
        border-top: 1px solid transparent;
        box-shadow: inset 0 1px 0 rgba(255,255,255,0.1);
    }
    .navbar-collapse.collapse {
        display: none!important;
    }
    .navbar-nav {
        float: none!important;
        margin: 7.5px -15px;
    }
    .navbar-nav>li {
        float: none;
    }
    .navbar-nav>li>a {
        padding-top: 10px;
        padding-bottom: 10px;
    }
    .collapse.in{
        display:block !important;
    }
}

Demo: http://bootply.com/UQaiG0oNTR 演示: http//bootply.com/UQaiG0oNTR

I suggest installing Bootstrap with SASS , in this way mixins are separeted and easier to manage. 我建议使用SASS安装Bootstrap ,这样可以使mixins分离并且易于管理。

With this configuiration the breaking-point is in your _variable mixins, and the _navbar mixin just picks up the variable by default. 通过此配置,断点位于_variable mixins中,而_navbar mixin默认情况下仅拾取变量。

Overriding will work but it's probably neater if you keep coherency with bootstrap mixins. 覆盖将起作用,但是如果您与Bootstrap Mixins保持一致,则可能会更整洁。

Read the documentation that you yourself linked to! 阅读您自己链接到的文档!

Overflowing content 内容溢出

Since Bootstrap doesn't know how much space the content in your navbar needs, you might run into issues with content wrapping into a second row . 由于Bootstrap不知道导航栏中的内容需要多少空间,因此您可能会遇到将内容包装到第二行的问题 To resolve this, you can: 要解决此问题,您可以:

  • Reduce the amount or width of navbar items. 减少导航栏项目的数量或宽度。
  • Hide certain navbar items at certain screen sizes using responsive utility classes. 使用响应实用程序类以某些屏幕尺寸隐藏某些导航栏项目。
  • Change the point at which your navbar switches between collapsed and horizontal mode. 更改导航栏在折叠和水平模式之间切换的点。 Customize the @grid-float-breakpoint variable or add your own media query. 自定义@grid-float-breakpoint变量或添加自己的媒体查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM