简体   繁体   English

滚动到顶部后,如何将导航栏的位置更改为固定?

[英]How can I change the position of my navbar to fixed after scrolling to the top?

I want to change my navbar position to fixed. 我想将导航栏位置更改为固定。

Here is my code: 这是我的代码:

 $(document).ready(function() { $(document).scroll(function() { var scroll = $(this).scrollTop(); var topDist = $("#container").position(); if (scroll > topDist.top) { $('nav').css({ "position": "fixed", "top": "0" }); } else { $('nav').css({ "position": "static", "top": "auto" }); } }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="container"> <nav> <ul> <li><a href="#">LINK 1</a></li> <li><a href="#">LINK 2</a></li> <li><a href="#">LINK 3</a></li> <li><a href="#">LINK 4</a></li> </ul> </nav> 

This is the demo link 这是演示链接

You can utilize the position: sticky property which is well supported now on most browsers: 您可以利用position: sticky属性,目前大多数浏览器都支持该属性:

#container {
  position: sticky;
  top: 0;
}

https://jsfiddle.net/wqp9430L/ https://jsfiddle.net/wqp9430L/

Edit: I want to shed some light on why your JS and overall solution doesn't work. 编辑:我想阐明为什么您的JS和整体解决方案不起作用。

Your JS isn't actually triggering because you're detecting scroll on the $(document) but your document and window isn't actually scrolling because your content is actually scrolling within .parallax . 您的JS实际上没有触发,因为您正在检测$(document)上的scroll ,但是您的文档和窗口实际上并未滚动,因为您的内容实际上是在.parallax滚动。 So instead of detecting the scroll on document you have to detect scroll on parallax : 因此,无需检测文档上的滚动,而必须检测parallax滚动:

$(document).ready(function() {
  $('.parallax').scroll(function () {
      var scroll = $(this).scrollTop();
      var topDist = $("#container").offset();

      if (scroll > topDist.top) {
          $('nav').css({"position":"fixed","top":"0"});
      } else {
          $('nav').css({"position":"static","top":"auto"});
      }
  });
});

Now while this works in terms of getting actually scroll values: https://jsfiddle.net/wqp9430L/2/ you'll notice there's a few issues. 现在,尽管这在获取实际滚动值方面起作用: https : //jsfiddle.net/wqp9430L/2/,您会注意到有一些问题。

  1. As you scroll the offset changes, so you should move topDist outside of the scroll function to get the initial value, but 滚动偏移量更改时,因此应将topDist移动到scroll功能之外以获取初始值,但是

  2. If the fiddle loads too quickly (before you images finishes rendering) topDist becomes in accurate, so you'll have to ensure topDist is calculated after the images are fully rendered, then there's also 如果小提琴加载得太快(在图像完成渲染之前) topDist变得准确,那么您必须确保在完全渲染图像之后计算topDist ,然后还要

  3. Changing your nav to position: fixed makes your nav fixed to the body but since you're not scrolling in the body but rather in .parallax you won't see the nav. nav更改为position: fixed使导航fixed在主体上,但是由于您不是在主体中滚动而是在.parallax滚动,因此您不会看到导航。

For these reason you should use the CSS solution, but if you insist on using JS you solution should look more like: 由于这些原因,您应该使用CSS解决方案,但是如果您坚持使用JS,则解决方案应该更像:

if (imgLoaded) {
  var topDist = $("#container").offset().top;

  $('.parallax').scroll(function () {
    var scroll = $(this).scrollTop();

    if (scroll > topDist) {
      $('nav').css({
        position: 'fixed',
        top: scroll + 'px'
      });
    } else {
      $('nav').css({
        position: 'static',
        top: 'auto'
      });
    }
  });
}

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

相关问题 如何在bootsrap 4中更改顶部固定导航栏的颜色? - How can i change color of top fixed navbar in bootsrap 4? 如何在bootstrap中滚动后创建一个scrollspy navbar-fixed-top? - How to create a scrollspy navbar-fixed-top after scrolling in bootstrap? 我一直在尝试使我的NAVBAR在滚动时固定在顶部。 但它仍然固定在中间 - I've been trying to make my NAVBAR fixed to the top when scrolling. But it remains fixed in the middle 如何在向下滚动时修复我的图像,它位于导航栏顶部,而文本不是 - How can i fix my images while scrolling down it comes on top of navbar while the texts are not jQuery:在页面滚动时更改固定顶部导航栏的文本颜色 - jQuery: change text color of a fixed top navbar at the scrolling of the page 请问我 position 我的导航栏如何固定和转换比例 function 工作 - Please how can i position my navbar fixed and transform scale function working Javascript固定式导航栏仅在滚动后 - Javascript fixed-top navbar only after scrolling 在MaterializeCSS中滚动时更改导航栏固定位置 - Change navbar-fixed position when scrolling in MaterializeCSS 如何使滚动折叠导航navbar-fixed-top? - How to make scrolling collapsing navigation navbar-fixed-top? 滚动后如何更改导航栏颜色? - How to change navbar colour after scrolling?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM