简体   繁体   English

将自定义导航栏转换为引导程序上的下拉菜单

[英]Converting custom nav bar to dropdown menu on bootstrap

I want to have the navigation bar I made convert to a dropdown-menu when viewed on a smaller screen (like phones and tablets). 我希望在较小的屏幕(如手机和平板电脑)上查看时,将导航栏转换成下拉菜单。 The problem is, I didn't use the conventional bootstrap nav bar system, and just made my own. 问题是,我没有使用常规的引导导航栏系统,而是自己制作了。 I know that the usual bootstrap solution involves using the nav classes, but I'm not really sure how I'd integrate those into my own code since I didn't use them to begin with. 我知道通常的引导程序解决方案涉及使用nav类,但是我真的不确定如何将它们集成到自己的代码中,因为我没有使用它们。 For tablets, I was hoping to have the main menu buttons hide into a dropdown menu with a toggle button, while keeping the logo there. 对于平板电脑,我希望将主菜单按钮隐藏在带有切换按钮的下拉菜单中,同时保留徽标。 Here is my code: 这是我的代码:

<header class="main-header" id="Navigation">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-3">
                <img class="HeaderLogo img-responsive" src="http://res.cloudinary.com/duocsoiqy/image/upload/v1499450095/Enpowered_Logo_k69cqw.png">
            </div>
            <div class="col-md-6 text-center">
                <div class="row text-center">
                    <div class="col-md-2 offset-md-1">
                        <p class="NavBarButtons"> WHY US </p>
                    </div>
                    <div class="col-md-2">
                        <p class="NavBarButtons"> FIX YOUR BILL </p>
                    </div>
                    <div class="col-md-2">
                        <p class="NavBarButtons"> HOW IT WORKS </p>
                    </div>
                    <div class="col-md-2">
                        <p class="NavBarButtons"> LEARN WITH US </p>
                    </div>
                    <div class="col-md-2">
                        <p class="NavBarButtons"> COMMERCIAL </p>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="row">
                    <div class="col-md-3 offset-md-4">
                        <p class="NavBarButtons"> LOG IN </p>
                    </div>
                    <div class="col-md-4 md-offset-3">
                        <button class="SignUp"><b> UNLOCK YOUR POWER </b></button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</header>

Since I used the grid system, everything collapses pretty well until a certain point, then the entire header completely messes up and stacks, which is why I wanted to try having the menu buttons go into a dropdown menu when that breakpoint is reached. 自从我使用网格系统以来,所有东西都很好地折叠到了某个点,然后整个标题完全弄乱了并堆积了,这就是为什么我想尝试在达到断点时将菜单按钮放入下拉菜单的原因。 Any help at all is much appreciated. 非常感谢任何帮助。 Thanks! 谢谢!

You lose a lot of baked-in functionality when you try to do this outside a .navbar but it's manageable. 当您尝试在.navbar之外执行此操作时,会丢失很多内置功能,但这是可管理的。 The .navbar-collapse component itself isn't directly tied to the use of the larger component; .navbar-collapse组件本身并不直接与使用较大的组件相关; you just need to include the <button> and wrapper element for the collapse: 您只需要包含<button>和wrapper元素即可折叠:

<header class="main-header" id="Navigation">
    <div class="container-fluid">
        <div class="row">
            <div class="col-xs-3">
                <img class="HeaderLogo img-responsive" src="http://res.cloudinary.com/duocsoiqy/image/upload/v1499450095/Enpowered_Logo_k69cqw.png">
            </div>

            <div class="col-xs-9 text-right">
                <div class="row">
                    <div class="navbar-header">
                        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#menu-collapse" aria-expanded="false">
                            <span class="sr-only">Toggle navigation</span>
                            <span class="icon-bar">-</span>
                            <span class="icon-bar">-</span>
                            <span class="icon-bar">-</span>
                        </button>
                    </div>

                    <div class="collapse navbar-collapse text-center" id="menu-collapse">
                        <div class="col-md-2"><p class="NavBarButtons">WHY US</p></div>
                        <div class="col-md-2"><p class="NavBarButtons">FIX YOUR BILL</p></div>
                        <div class="col-md-2"><p class="NavBarButtons">HOW IT WORKS</p></div>
                        <div class="col-md-2"><p class="NavBarButtons">LEARN WITH US</p></div>
                        <div class="col-md-2"><p class="NavBarButtons">COMMERCIAL</p></div>
                        <div class="col-md-1"><p class="NavBarButtons">LOG IN</p></div>
                        <div class="col-md-1"><button class="SignUp"><b>UNLOCK YOUR POWER</b></button></div>
                    </div>
                </div>
            </div>

        </div>
    </div>
</header>

... ...

Where you run into problems (and why I would recommend using the whole .navbar structure, is that you need to style the aforementioned button and its various states. You'll also need to address spacing concerns (the dropdown pushes elements down in this fashion). 遇到问题的地方(以及为什么我建议使用整个.navbar结构的原因是,您需要设置上述按钮及其各种状态的样式。还需要解决间距问题(下拉菜单以这种方式将元素向下压)。

It's pretty nasty, but.. You could do it with some adjustments to your code so that you'll end up with this: 这很讨厌,但是..您可以通过对代码进行一些调整来做到这一点,从而最终得到以下结果:

<nav class="main-header navbar navbar-default" id="Navigation">
    <div class="container-fluid">
        <div class="col-md-3 navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">
                <img class="HeaderLogo img-responsive" src="http://res.cloudinary.com/duocsoiqy/image/upload/v1499450095/Enpowered_Logo_k69cqw.png" style="width: auto;">
            </a>
        </div>
        <div class="col-md-6 text-center collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="text-center nav navbar-nav">
                <li class="col-md-2 offset-md-1">
                    <p class="NavBarButtons"> WHY US </p>
                </li>
                <li class="col-md-2">
                    <p class="NavBarButtons"> FIX YOUR BILL </p>
                </li>
                <li class="col-md-2">
                    <p class="NavBarButtons"> HOW IT WORKS </p>
                </li>
                <li class="col-md-2">
                    <p class="NavBarButtons"> LEARN WITH US </p>
                </li>
                <li class="col-md-2">
                    <p class="NavBarButtons"> COMMERCIAL </p>
                </li>
            </ul>
        </div>
        <div class="col-md-3">
            <div class="row">
                <div class="col-md-3 offset-md-4">
                    <p class="NavBarButtons"> LOG IN </p>
                </div>
                <div class="col-md-4 md-offset-3">
                    <button class="SignUp"><b> UNLOCK YOUR POWER </b></button>
                </div>
            </div>
        </div>
    </div>
</nav>

Here's what I did: 这是我所做的:

  1. give the col-md-3 class navbar-header col-md-3navbar-header
  2. In there add 在其中添加

      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> 
  3. Add <a class="navbar-brand" href="#"> around your img 在您的img周围添加<a class="navbar-brand" href="#">

  4. the col-md-6 column, add classes collapse navbar-collapse and id="bs-example-navbar-collapse-1" col-md-6列中,添加类collapse navbar-collapseid="bs-example-navbar-collapse-1"

  5. The div in that one add classes nav navbar-nav 其中的div添加类nav navbar-nav

  6. Make the header a nav element header设为nav元素

  7. Give the nav classes navbar navbar-default nav类navbar navbar-default

  8. Remove the row 删除行

  9. The div in the col-md-6 column, remove the row and make it an ul Turn the div s in there into li s col-md-6列中的div删除,并将其设置为ul将其中的div转换为li s

Basically what you have done now is take the steps necessary to write a bootstrap nav. 基本上,您现在要做的就是采取必要的步骤来编写引导导航。 Take a good look at the example at http://getbootstrap.com/components/#navbar You'll see that it actually doesn't use the grid system. 仔细看一下http://getbootstrap.com/components/#navbar上的示例,您将看到它实际上并不使用网格系统。

Keep in mind you need to include the bootstrap javascript to be able to use toggle functionality. 请记住,您需要包含引导Javascript才能使用toggle功能。

<script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>

Also also, don't put p elements in your navigation. 另外,也不要在导航中放置p元素。 Paragraphs don't make any sense there. 段落在那里没有任何意义。

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

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