简体   繁体   English

Twitter Bootstrap 3导航栏崩溃

[英]Twitter Bootstrap 3 navbar collapse

I'm trying to migrate my Twitter Bootstrap 2 navbar to Bootstrap 3. I have 3 parts in my navbar. 我正在尝试将我的Twitter Bootstrap 2导航栏迁移到Bootstrap 3.我的导航栏中有3个部分。

"myContent1" and "myContent2" will always be visible. “myContent1”和“myContent2”将始终可见。 "myContent3" will be collapsed for small screens. 对于小屏幕,“myContent3”将被折叠。 But when I click stacked button in a small screen device, collapsed menu doesn't opened. 但是当我在小屏幕设备中单击堆叠按钮时,折叠菜单不会打开。 Nothing happens when I click button. 单击按钮时没有任何反应。

I put it into a fiddle: http://jsfiddle.net/mavent/LDP8K/6/ 我把它放进一个小提琴: http//jsfiddle.net/mavent/LDP8K/6/

<nav class="navbar navbar-inverse navbar-fixed-top navbar-sam-main" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">

    <a class="navbar-brand navbar-left" href="" title="aaaa" id="MyContent1">
        MySite
    </a>

    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-mycol">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
    </button>



    <ul class="nav navbar-nav navbar-left" id="myContent2">
        <li class="dropdown" id="users">
            <a class="navbar-brand" href="" title="aaa" style="display:inline-block;padding-right:0px;">
                <img src="" width="24" height="24" alt="aaaa">
                MyName
            </a>
            <a class="dropdown-toggle" data-toggle="dropdown" href="#users"  style="display:inline-block;padding-left:0px;">
                <b class="caret" style="border-left: 6px solid transparent; border-right: 6px solid transparent;  border-top: 6px solid #999;"></b>
            </a>
            <ul class="dropdown-menu">
                <li><a href=""><img src="" width="24" height="30"/> OtherName</a></li>
                <li class="divider"></li>
                <li><a href="">AAAA</a></li>
                <li><a href="">BBB</a></li>
            </ul>
        </li>
    </ul>


</div>


<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-mycol">
    <ul class="nav navbar-nav navbar-right" id="myContent3">

        <li><a href="">UUUU</a></li>
        <li><a href="">YYYYY</a></li>

        <li class="dropdown" id="wer">
            <a class="dropdown-toggle" data-toggle="dropdown" href="#wer">Tools
                <b class="caret"></b>
            </a>
            <ul class="dropdown-menu">
                <li><a href="">aaaa</a></li>
                <li><a href="">bbbb</a></li>
                <li><a href="">cccc</a></li>
            </ul>
        </li>

    </ul>

</div><!-- /.navbar-collapse -->
</nav>

Edit: With one person's suggestion I put "toggle button", out of the navbar-header section. 编辑:在一个人的建议下,我将“切换按钮”放在navbar-header部分之外。 And I gave button top:0 and right:0 absolute style to the button. 我给了按钮top:0 and right:0按钮的绝对样式。 But In that case, if screen is very small, myContent2 penetrates into button. 但在这种情况下,如果屏幕非常小, myContent2进入按钮。 You can see that behaviour from this: http://jsfiddle.net/mavent/paXt9/2/ 你可以从中看到这种行为: http//jsfiddle.net/mavent/paXt9/2/

Edit2: I need this behaviour: Edit2:我需要这个行为:

For big screens, everthing is visible. 对于大屏幕,可以看到外观。
在此输入图像描述

For smaller screens, myContent2 is visible, myContent3 is collapsed. 对于较小的屏幕, myContent2可见, myContent3已折叠。 myContent3 is uncollapsed if button is clicked. 如果单击按钮,则myContent3未展开。
在此输入图像描述

For very small screens, myContent2 will be shown in next line. 对于非常小的屏幕, myContent2将在下一行显示。 So myContent2 and button will not be conflicted each other's place. 所以myContent2和按钮不会互相冲突。
在此输入图像描述

The dropdown element is taking up the whole navbar width and is preventing the button from working, to fix things you can set a higher z-index to your brand link and to the navbar-toggle button: 下拉元素占用整个导航栏宽度并阻止按钮工作,修复可以为品牌链接和导航栏切换按钮设置更高z-index的内容:

.navbar-sam-main .navbar-toggle, .navbar-sam-main .navbar-brand {
    position: relative;
    z-index:1;
}

For the other issues that were brought up in the comments section, the fixes were to wrap the dropdown link and the caret in a span to make them wrap as a single element and using a media query to have the link display in the center when it falls to a second line: 对于在评论部分提出的其他问题,修复方法是将下拉链接和插入符号包装在一个范围中,以使它们作为单个元素包装,并使用媒体查询将链接显示在中心时落到第二行:

HTML HTML

<li class="dropdown" id="users"> 
    <span>
        <a class="navbar-link" href="" title="aaa" style="display:inline-block;padding-right:0px;">
            <img src="" width="24" height="24" alt="aaaa" />
            MyName
        </a>
        <a class="dropdown-toggle" data-toggle="dropdown" href="#users"  style="display:inline-block;padding-left:0px;">
            <b class="caret" style="border-left: 6px solid transparent; border-right: 6px solid transparent;  border-top: 6px solid #999;"></b>
        </a>
    </span>
    <ul class="dropdown-menu">
        ...
    </ul>
</li>

CSS CSS

.navbar-sam-main .navbar-nav > li >span {
    display: inline-block;
    padding-top: 10px;
    padding-bottom: 10px;
    line-height: 20px;
}
.navbar-sam-main .navbar-nav > li >span .navbar-brand {
    padding-top: 0;
    padding-bottom: 0;
}

@media (max-width: 270px) {
    #myContent1 > li.dropdown {
        text-align: center;
    }
    #myContent1 > li.dropdown >ul {
        text-align:left;
    }
}

I think you should use responsive utility classes, with screen width breakpoints, such as: 我认为你应该使用具有屏幕宽度断点的响应式实用程序类,例如:

.hidden-sm
.visible-lg

More info: http://getbootstrap.com/css/#responsive-utilities 更多信息: http//getbootstrap.com/css/#responsive-utilities

Try not to override layout mechanisms provided with Bootstrap and create your layout in the first state initially, then override visibility with the classes I've mentioned. 尽量不要覆盖随Bootstrap提供的布局机制,并在最初的状态下创建布局,然后使用我提到的类覆盖可见性。

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

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