bootstrap static to fixed navbar jump on scroll

时间:2015-10-08 17:09:55

标签: javascript jquery html css twitter-bootstrap

在解决这个问题时遇到一些麻烦。 当观众滚过顶部的徽标时,我有一个从静态到固定的引导导航栏。一切正常,除非导航栏到达顶部,它会跳过前几行内容。任何人都可以帮我解决这个问题吗?

HTML

<div id="logo">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <h1>BIG,</h1>
                <h1>BIG LOGO!</h1>
            </div>
        </div>
    </div>
</div>    

<div id="nav-wrapper">
    <div id ="nav" class="navbar navbar-inverse navbar-static-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <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="#">Project name</a>
        </div>
        <div id="navbar" 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>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>
</div>    

CSS

#nav.affix {
    position: fixed;
    top: 0;
    width: 100%
}

的java

$(function() {
    $('#nav-wrapper').height($("#nav").height());

    $('#nav').affix({
        offset: { top: $('#nav').offset().top }
    });
});

Check the JSFIDDLE example

谢谢你们!

1 个答案:

答案 0 :(得分:0)

$(function() {
    $('#nav-wrapper').height($("#nav").height());

    $('#nav').affix({
        offset: { top: $('#nav').offset().top }
    });

    $('#nav').on('affixed.bs.affix', function () {
        $('#content').css({"margin-top": "100px"})
    });
});

https://jsfiddle.net/yyLbgh98/1/