简体   繁体   English

当导航栏固定到屏幕顶部时,阻止内容“跳转”

[英]stopping content from 'jumping' when navbar fixes to top of screen

I have a site that utilizes the Bootstrap 3 navbar. 我有一个利用Bootstrap 3导航栏的网站。 It is positioned 280px below a block div and sticks to the top of the page when scrolled to that point 它位于块div下方280像素处,并滚动到该点时停留在页面顶部

HTML ( in < head > tags ) HTML(在<head>标记中)

<script>
  $(document).ready(function() {
  var s = $("#nav");
  var pos = s.position();                    
  $(window).scroll(function() {
    var windowpos = $(window).scrollTop();
    if (windowpos >= pos.top) {
        s.addClass("stick");
    } else {
        s.removeClass("stick"); 
    }
  });
});

HTML HTML

<div id="nav">
  <div class="navbar navbar-default navbar-static">
     <!-- .btn-navbar is used as the toggle for collapsed navbar content -->

...

CSS CSS

header {
  height:280px;
}

.stick {
  position:fixed;
  top:0px;
  width: 100%;
}

It sticks to the page when it's scrolled to the way it's supposed to. 当滚动到其应有的方式时,它会粘在页面上。 BUT when the 'nav' div gets the position:fixed attribute applied, it no longer is in the content flow and the content 'jumps' up the same height as the height of the nav. 但是,当'nav'div应用了position:fixed属性时,它不再位于内容流中,并且内容'跳跃'到与nav高度相同的高度。

How can I keep the content from jumping? 如何防止内容跳动?

Create a wrapping element around your header. 在标头周围创建一个包装元素。 Apply same height to the wrapper. 对包装纸施加相同的高度。 Now if you make your header fixed, the wrapper element will still be there, occupying the same height 现在,如果您将标头固定,则wrapper元素仍将存在,并占据相同的高度

here's an example 这是一个例子

  $(document).ready(function() { var s = $("#nav"); var pos = s.position(); $(window).scroll(function() { var windowpos = $(window).scrollTop(); if (windowpos >= pos.top) { s.addClass("stick"); } else { s.removeClass("stick"); } }); }); 
 body {margin:0} #nav, .nav-wrapper { height:100px; background: gray; } .stick { position:fixed; top:0px; width: 100%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> <div class="nav-wrapper"> <div id="nav"> <div class="navbar navbar-default navbar-static">Header</div> </div> </div> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> Content content content<br> 

Don't create a custom class like sticky for it use navbar-fixed-top class like, 不要创建像sticky这样的自定义类,因为它要使用navbar-fixed-top类,

if (windowpos >= pos.top) {
    s.addClass("navbar-fixed-top");
} else {
    s.removeClass("navbar-fixed-top"); 
}

Refer 参考

I had this issue too recently. 我最近也有这个问题。 I solved it by putting a position:absolute div behind my nav and using that as my scroll point reference for the addClass. 我通过在我的导航后面放置一个position:absolute div并将其用作addClass的滚动点引用来解决此问题。 Honestly I'm not quite sure why it worked, but it did! 老实说,我不太确定为什么会这样,但确实如此!

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

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