简体   繁体   English

从静态引导到固定导航栏,滚动滚动

[英]bootstrap static to fixed navbar jumping on scroll

Having a little trouble working this one out. 解决这个问题有点麻烦。 I have a bootstrap navbar that goes from static to fixed when the viewer scrolls past the logo at the top. 我有一个引导导航栏,当查看者滚动到顶部的徽标时,该导航栏从静态变为固定。 Everything works fine, except when the navbar reaches the top, it jumps down missing first few lines of content. 一切正常,除非导航栏到达顶部,否则它会跳下并丢失前几行内容。 Can anyone help me solve this? 谁能帮我解决这个问题?

html 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 的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 查看JSFIDDLE示例

Thanks guys! 多谢你们!

$(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/ https://jsfiddle.net/yyLbgh98/1/

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

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