jquery - 子导航栏与 Twitter Bootstrap 一起折叠

标签 jquery css twitter-bootstrap

我正在使用 twitter bootstrap,我的导航栏可以正常使用折叠功能。

这是我的 html 代码:

<div class="subnav">
    <ul class="nav nav-pills">
      <li class="dropdown">
        <a href="#" data-toggle="dropdown" class="dropdown-toggle">Buttons <b class="caret"></b></a>
        <ul class="dropdown-menu">
          <li class=""><a href="#buttonGroups">Button groups</a></li>
          <li class=""><a href="#buttonDropdowns">Button dropdowns</a></li>
        </ul>
      </li>
      <li class="dropdown">
        <a href="#" data-toggle="dropdown" class="dropdown-toggle">Navigation <b class="caret"></b></a>
        <ul class="dropdown-menu">
          <li class=""><a href="#navs">Nav, tabs, pills</a></li>
          <li class=""><a href="#navbar">Navbar</a></li>
          <li class=""><a href="#breadcrumbs">Breadcrumbs</a></li>
          <li class=""><a href="#pagination">Pagination</a></li>
        </ul>
      </li>
      <li class=""><a href="#labels">Labels</a></li>
      <li class=""><a href="#badges">Badges</a></li>
      <li class=""><a href="#typography">Typography</a></li>
      <li class=""><a href="#thumbnails">Thumbnails</a></li>
      <li class=""><a href="#alerts">Alerts</a></li>
      <li><a href="#progress">Progress bars</a></li>
      <li><a href="#misc">Miscellaneous</a></li>
    </ul>
  </div>

这是我的bootstrap_and_overrides.css.scss

$iconSpritePath: asset-url('glyphicons-halflings.png', image);
$iconWhiteSpritePath: asset-url('glyphicons-halflings-white.png', image);
@import "bootstrap";
body { padding-top: 40px;
       .navbar .brand {padding: 10px 20px; }
       background: url("background.png") repeat scroll 0 0 #EEEEEE;


.subnav {
    background-color: #EEEEEE;
    background-repeat: repeat-x;
    border: 1px solid #E5E5E5;
    border-radius: 4px 4px 4px 4px;
    height: 36px;



            .nav > li > a {
                        border-left: 1px solid #F5F5F5;
                        border-radius: 0 0 0 0;
                        border-right: 1px solid #E5E5E5;
                        margin: 0;
                        padding-bottom: 11px;
                        padding-top: 11px;
                        }
    }

    .subnav-fixed {
    /*important part*/
    left: 0;
    position: fixed;
    right: 0;
    top: 40px;
    z-index: 1020;
    /*design stuff*/
    border-color: #D5D5D5;
    border-radius: 0 0 0 0;
    border-width: 0 0 1px;
    box-shadow: 0 1px 0 #FFFFFF inset, 0 1px 5px rgba(0, 0, 0, 0.1);
    background-color: #FFFFFF;
    }
}
@import "bootstrap-responsive";

这是我的 js 代码:

$(document).scroll(function(){
    // If has not activated (has no attribute "data-top"
    if (!$('.subnav').attr('data-top')) {
        // If already fixed, then do nothing
        if ($('.subnav').hasClass('subnav-fixed')) return;
        // Remember top position
        var offset = $('.subnav').offset()
        $('.subnav').attr('data-top', offset.top);
    }

    if ($('.subnav').attr('data-top') - $('.subnav').outerHeight() <= $(this).scrollTop())
        $('.subnav').addClass('subnav-fixed');
    else
        $('.subnav').removeClass('subnav-fixed');
});

另外,我正在使用子导航栏,我的问题是我的子导航栏没有折叠。

twitter bootstrap 中正确的子导航栏版本:

fine subnav twitter bootstrap

我的糟糕版本无法正常工作:/

bad subnav twitter bootstrap

如何解决此问题?

谢谢!

最佳答案

如果您遇到此问题,可以通过以下方式解决此问题:

在您的 javascript 文件中,您必须添加以下 jquery 代码:

//subnav config
 $(function(){

    var $win = $(window);
    var $nav = $('.subnav');
    var navTop = $('.subnav').length && $('.subnav').offset().top - 38;
    var isFixed = 0;

    processScroll();

    $win.on('scroll', processScroll);

    function processScroll() {
        console.log('test');
        var i, scrollTop = $win.scrollTop();
        if (scrollTop >= navTop && !isFixed) {
            isFixed = 1;
            $nav.addClass('subnav-fixed');
        } else if (scrollTop <= navTop && isFixed) {
            isFixed = 0;
            $nav.removeClass('subnav-fixed');
        }
    };
})

在您的 css 文件或文件 bootstrap_and_overrides.css.scss 中,您可以添加以下代码:

/* Add additional stylesheets below
-------------------------------------------------- */

/* Subnav */
.subnav {
  width: 100%;
  height: 36px;
  background-color: #eeeeee; /* Old browsers */
  background-repeat: repeat-x; /* Repeat the gradient */
  background-image: -moz-linear-gradient(top, #f5f5f5 0%, #eeeeee 100%); /* FF3.6+ */
  background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#f5f5f5), color-stop(100%,#eeeeee)); /* Chrome,Safari4+ */
  background-image: -webkit-linear-gradient(top, #f5f5f5 0%,#eeeeee 100%); /* Chrome 10+,Safari 5.1+ */
  background-image: -ms-linear-gradient(top, #f5f5f5 0%,#eeeeee 100%); /* IE10+ */
  background-image: -o-linear-gradient(top, #f5f5f5 0%,#eeeeee 100%); /* Opera 11.10+ */
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f5f5f5', endColorstr='#eeeeee',GradientType=0 ); /* IE6-9 */
  background-image: linear-gradient(top, #f5f5f5 0%,#eeeeee 100%); /* W3C */
  border: 1px solid #e5e5e5;
  -webkit-border-radius: 4px;
     -moz-border-radius: 4px;
          border-radius: 4px;
}
.subnav .nav {
  margin-bottom: 0;
}
.subnav .nav > li > a {
  margin: 0;
  padding-top:    11px;
  padding-bottom: 11px;

  border-right: 1px solid #e5e5e5;
  -webkit-border-radius: 0;
     -moz-border-radius: 0;
          border-radius: 0;
}
.subnav .nav > .active > a,
.subnav .nav > .active > a:hover {
  padding-left: 13px;
  color: #777;
  background-color: #e9e9e9;
  border-right-color: #ddd;
  border-left: 0;
  -webkit-box-shadow: inset 0 3px 5px rgba(0,0,0,.05);
     -moz-box-shadow: inset 0 3px 5px rgba(0,0,0,.05);
          box-shadow: inset 0 3px 5px rgba(0,0,0,.05);
}
.subnav .nav > .active > a .caret,
.subnav .nav > .active > a:hover .caret {
  border-top-color: #777;
}
.subnav .nav > li:first-child > a,
.subnav .nav > li:first-child > a:hover {
  border-left: 0;
  padding-left: 12px;
  -webkit-border-radius: 4px 0 0 4px;
     -moz-border-radius: 4px 0 0 4px;
          border-radius: 4px 0 0 4px;
}
.subnav .nav > li:last-child > a {
  border-right: 0;
}
.subnav .dropdown-menu {
  -webkit-border-radius: 0 0 4px 4px;
     -moz-border-radius: 0 0 4px 4px;
          border-radius: 0 0 4px 4px;
}


/* Fixed subnav on scroll, but only for 980px and up (sorry IE!) */
@media (min-width: 980px) {
  .subnav-fixed {
    position: fixed;
    top: 40px;
    left: 0;
    right: 0;
    z-index: 1020; /* 10 less than .navbar-fixed to prevent any overlap */
    border-color: #d5d5d5;
    border-width: 0 0 1px; /* drop the border on the fixed edges */
    -webkit-border-radius: 0;
       -moz-border-radius: 0;
            border-radius: 0;
    -webkit-box-shadow: inset 0 1px 0 #fff, 0 1px 5px rgba(0,0,0,.1);
       -moz-box-shadow: inset 0 1px 0 #fff, 0 1px 5px rgba(0,0,0,.1);
            box-shadow: inset 0 1px 0 #fff, 0 1px 5px rgba(0,0,0,.1);
    filter: progid:DXImageTransform.Microsoft.gradient(enabled=false); /* IE6-9 */
  }
  .subnav-fixed .nav {
    width: 100%;
    margin: 0 auto;
    padding: 0 1px;
    background-color: #FFFFFF;
  }
  .subnav .nav > li:first-child > a,
  .subnav .nav > li:first-child > a:hover {
    -webkit-border-radius: 0;
       -moz-border-radius: 0;
            border-radius: 0;
  }
}


@media (max-width: 768px) {

  /* Subnav */
  .subnav {
    position: static;
    top: auto;
    z-index: auto;
    width: auto;
    height: auto;
    background: #fff; /* whole background property since we use a background-image for gradient */
    -webkit-box-shadow: none;
       -moz-box-shadow: none;
            box-shadow: none;
  }
  .subnav .nav > li {
    float: none;
  }
  .subnav .nav > li > a {
    border: 0;
  }
  .subnav .nav > li + li > a {
    border-top: 1px solid #e5e5e5;
  }
  .subnav .nav > li:first-child > a,
  .subnav .nav > li:first-child > a:hover {
      -webkit-border-radius: 4px 4px 0 0;
         -moz-border-radius: 4px 4px 0 0;
              border-radius: 4px 4px 0 0;
  }


}

如果此代码对您有效,请接受答案并对此回复给予积极反馈。我希望有帮助。

非常感谢。

关于jquery - 子导航栏与 Twitter Bootstrap 一起折叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10318163/

相关文章:

css - 当类名只是一个整数时,如何编写 CSS 选择器?

css - 使用 django 压缩器发布压缩 sass

javascript - 减速时机

php - 在 WordPress 的当前菜单项中添加一个类来定位标签

javascript - 放大图像 (JavaScript)

javascript - Bootstrap 模式不适用于示例代码

html - Bootstrap CSS 对齐一个分区类

css - Bootstrap : dropdown menu covers content

javascript - 如何将reCaptcha v2与JS和PHP表单集成?

php - 如何复制 MySQL 填充的选择元素?