Bootstrap导航栏不起作用

时间:2016-02-04 21:46:16

标签: html css twitter-bootstrap twitter-bootstrap-3 nav

这只是我关于堆栈溢出的第二个问题,所以如果您需要任何其他信息或需要澄清我所说的任何内容,请不要犹豫。 我试着调查这个问题,确实找到了一些有类似问题的人,但没有一个帮助他们的修复工作对我有效。

我希望在max width超过767px.时隐藏我的导航栏当它低于该像素数量时,它应该成为一个应该崩溃的下拉菜单单击按钮时展开。这对我来说似乎没有用。

我的代码如下

HTML

<header class="pull-center" id="masthead">
  <nav class="navbar navbar-fixed-top">
    <div class="navbar-inner">
      <div class="container">
        <div id="img1"></div>
        <div id="img2"></div>
        <div id="img3"></div>
        <div id="img4"></div>
        <a class="btn btn-navbar" data-target=".nav-collapse" data-toggle="collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <a class="brand brand-ctg" href="#section-1"><span></span></a>
            CTG
            <span></span>
        </a>
        <div class="nav-collapse">
            <ul class="nav">
                <li><a href="#section-2">Demo Reel</a></li>
                <li><a href="#section-3">Portfolio</a></li>
                <li><a class="brand brand-ctg" href="#section-1"> <span></span></a></li>
                <li><a href="#section-4">Contact</a></li>
                <li><a href="http://www.colortheblog.com" target="_blank">Blog</a></li>
            </ul>
        </div>
      </div>
    </div>
  </nav>
</header>

CSS

    @media (max-width: 767px) {
    .nav-collapse .nav > li {
        float: left !important;
    }
    .navbar .in .nav li {
       display: block;
       float: none;
       width:100%;
    }
    .navbar .nav > li > a {
        color: #fff !important;
        float: none;
        margin-right: -3px !important;
        padding: 0 40px 10px !important;
        text-decoration: none;
    }
    .navbar .nav {
        margin-top: 0 !important;
        position: fixed!important;
        z-index: 1;
    }
    .nav-collapse .brand {
        display: block !important;
    }
    .navbar .nav .active a,
    .navbar .nav > li > a:hover,
    .navbar .nav > .active > a:hover,
    .navbar .nav > .active > a:focus {
        background-color: transparent;
        box-shadow: none;
        color: #2d2e2d !important;
    }
    .navbar-fixed-top,
    .navbar-fixed-bottom,
    .navbar-static-top {
        margin-left: -20px;
        margin-right: -20px;
    }
    .navbar-fixed-top,
    .navbar-fixed-bottom {
        position: static;
    }
    .navbar-fixed-top {
        margin-bottom: 30px;
    }
    .navbar-fixed-bottom {
        margin-top: 30px;
    }
    .navbar-fixed-top .navbar-inner,
    .navbar-fixed-bottom .navbar-inner {
        padding: 5px;
    }
    .navbar .container {
        width: auto;
        padding: 0;
    }
    .navbar .brand {
        margin: -10px 40px 0 0;
    }
    .nav-collapse {
        clear: both;
    }
    .nav-collapse .nav {
        float: none;
        margin: 0 0 15px;
    }
    .nav-collapse .nav > li {
        float: none;
    }
    .nav-collapse .nav > li > a {
        margin-bottom: 2px;
    }
    .nav-collapse .nav > .divider-vertical {
        display: none;
    }
    .nav-collapse .nav .nav-header {
        color: #555555;
        text-shadow: none;
    }
    .nav-collapse .nav > li > a,
    .nav-collapse .dropdown-menu a {
        padding: 9px 15px;
        font-weight: bold;
        color: #555555;
        -webkit-border-radius: 3px;
        -moz-border-radius: 3px;
        border-radius: 3px;
    }
    .nav-collapse .btn {
        padding: 4px 10px 4px;
        font-weight: normal;
        -webkit-border-radius: 0px;
        -moz-border-radius: 0px;
        border-radius: 0px;
    }
    .nav-collapse .dropdown-menu li + li a {
        margin-bottom: 2px;
    }
    .nav-collapse .nav > li > a:hover,
    .nav-collapse .dropdown-menu a:hover {
        background-color: #f2f2f2;
    }
    .navbar-inverse .nav-collapse .nav > li > a,
    .navbar-inverse .nav-collapse .dropdown-menu a {
        color: #999999;
    }
    .navbar-inverse .nav-collapse .nav > li > a:hover,
    .navbar-inverse .nav-collapse .dropdown-menu a:hover {
        background-color: #111111;
    }
    .nav-collapse.in .btn-group {
        margin-top: 5px;
        padding: 0;
    }
    .nav-collapse .dropdown-menu {
        position: static;
        top: auto;
        left: auto;
        float: none;
        display: none;
        max-width: none;
        margin: 0 15px;
        padding: 0;
        background-color: transparent;
        border: none;
        -webkit-border-radius: 0;
        -moz-border-radius: 0;
        border-radius: 0;
        -webkit-box-shadow: none;
        -moz-box-shadow: none;
        box-shadow: none;
    }
    .nav-collapse .open > .dropdown-menu {
        display: block;
    }

    .nav-collapse .dropdown-menu:before,
    .nav-collapse .dropdown-menu:after {
        display: none;
    }
    .nav-collapse .dropdown-menu .divider {
        display: none;
    }
    .nav-collapse .nav > li > .dropdown-menu:before, .nav-collapse .nav > li > .dropdown-menu:after {
        display: none;
    }
    .nav-collapse .navbar-form,
    .nav-collapse .navbar-search {
        float: none;
        padding: 15px 15px;
        margin: 15px 0;
        border-top: 1px solid #f2f2f2;
        border-bottom: 1px solid #f2f2f2;
        -webkit-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 0 rgba(255, 255, 255, 0.1);
        -moz-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 0 rgba(255, 255, 255, 0.1);
        box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 0 rgba(255, 255, 255, 0.1);
    }
    .navbar-inverse .nav-collapse .navbar-form,
    .navbar-inverse .nav-collapse .navbar-search {
        border-top-color: #111111;  
        border-bottom-color: #111111;
    }
    .navbar .nav-collapse .nav.pull-right {
        float: none;
        margin-left: 0;
    }
    .nav-collapse,
    .nav-collapse.collapse {
        overflow: hidden;
        height: 0;
        background: #3b3b3b;
    }
    .navbar .btn-navbar {
        background:url(http://www.colorthegrayscale.com/images/icons/hover-teal.png);
        display: block;
        width: 25px;
        height: 50px;
        position: fixed;
    }
    .navbar-static .navbar-inner {
        padding-left: 10px;
        padding-right: 10px;
    }
    .navbar .brand.brand-ctg span {
        display: none !important;
    }

编辑1 试图解决它,但仍然有我的问题。我知道有些CSS让切换按钮不起作用,我似乎无法弄清楚是什么。为了在一个地方简单展示我的所有作品,您可以找到测试网站 here

1 个答案:

答案 0 :(得分:1)

当你不需要时,你会覆盖bootstrap CSS样式,所以这里有一个基于twitter bootstrap的简单navbar的例子:

<强> companion object

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />

<!-- Fixed navbar -->
<div class="navbar navbar-default navbar-fixed-top">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Project name</a>
    </div>
    <div class="collapse navbar-collapse">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Home</a>
        </li>
        <li><a href="#about">About</a>
        </li>
        <li><a href="#contact">Contact</a>
        </li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a>
            </li>
            <li><a href="#">Another action</a>
            </li>
            <li><a href="#">Something else here</a>
            </li>
            <li class="divider"></li>
            <li class="dropdown-header">Nav header</li>
            <li><a href="#">Separated link</a>
            </li>
            <li><a href="#">One more separated link</a>
            </li>
          </ul>
        </li>
      </ul>
    </div>
    <!--/.nav-collapse -->
  </div>
</div>

请参阅Twitter Bootstrap Bootply DEMO

编辑:根据您的评论/更新的问题

这是你的问题:

<a class="btn btn-navbar" data-target=".nav-collapse" data-toggle="collapse">
                <!-- <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span> -->
                <a class="brand brand-ctg" href="#section-1"><span>CTG</span></a>
</a>

您正在另一个a内打开a,这是无效的HTML。以下是如何修复它(根据文档):

<button type="button" class="btn btn-navbar" data-target=".nav-collapse" data-toggle="collapse">
  <span class="icon-bar"></span>
  <span class="icon-bar"></span>
  <span class="icon-bar"></span>
</button>
<a class="brand brand-ctg" href="#section-1"><span>CTG</span></a>

EDIT2:

我在寻找你正在使用旧版bootstrap.css(v.2.2.1),最新的稳定版是3.3.6,你错过了bootstrap.js(这使得菜单成为可能在崩溃时触发)