简体   繁体   English

在 Bootstrap 中使滚动导航栏粘在浏览器顶部

[英]Make Scrolling NavBar Stick At Top Of Browser In Bootstrap

I'm new to Twitter Bootstrap and want to have a NavBar at the bottom of a deep'ish MastHead and when the user vertically scrolls the page and the NavBar gets to the top of the page, change the style to so that the NavBar becomes Fixed.我是 Twitter Bootstrap 的新手,希望在深的 MastHead 底部有一个 NavBar,当用户垂直滚动页面并且 NavBar 到达页面顶部时,将样式更改为固定的。 The effect I want is the same as http://www.happycog.com/ only using the Bootstrap NavBar.我想要的效果和http://www.happycog.com/一样,只使用 Bootstrap NavBar。

So, the real question is how to trigger the navbar-fixed-top style when the NavBar is scrolled to position 0 ie: the top of the browser.所以,真正的问题是当 NavBar 滚动到位置 0 时如何触发 navbar-fixed-top 样式,即:浏览器的顶部。

Any ideas or pointers would be appreciated.任何想法或指针将不胜感激。 Thank you.谢谢你。

The CSS class navbar-fixed-top is what you want to add. CSS 类navbar-fixed-top是您想要添加的。

Here's an example:下面是一个例子:

<nav class='navbar navbar-inverse navbar-fixed-top sticky' role='navigation'>
    <!-- nav content -->
</nav>

You'll have to use javascript to do this.您必须使用 javascript 来执行此操作。 Here's a solution that relies on jQuery to convert the navbar positioning to fixed once it gets to the top of the page as well as stick a temporary div in its place to prevent the layout from changing.这是一个解决方案,它依赖于 jQuery 将导航栏定位转换为固定,一旦它到达页面顶部,并在其位置粘贴一个临时 div 以防止布局发生变化。

http://jsfiddle.net/T6nZe/ http://jsfiddle.net/T6nZe/

You can try CSS3 sticky position:您可以尝试使用 CSS3 粘贴位置:

.sticky {
  position: sticky;
  top: 10px; /* When the element reaches top: 10px, it becomes fixed. */
  z-index: 100;
}

Browser support: http://caniuse.com/#feat=css-sticky浏览器支持: http : //caniuse.com/#feat=css-sticky

我相信这就是你要找的: http : //www.w3schools.com/bootstrap/bootstrap_affix.asp

In aiming for browser compatibility, the following might be of more use.为了实现浏览器兼容性,以下内容可能更有用。

.sticky {
  position: -webkit-sticky;
  position: -moz-sticky;
  position: -ms-sticky;
  position: -o-sticky;
  position: sticky;
  top: 10px;
  z-index: 100;
}

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

相关问题 滚动到位置时,使导航条保持固定 - Make navbar stick upon scrolling to position 通过滚动将表格行粘贴到浏览器顶部 - Stick table row to the top of the browser by scrolling 滚动时使元素停留在顶部 - Make element stick to top when scrolling 使导航栏贴在顶部,但是当单击通知栏中的十字按钮时,它应该被删除并且导航栏贴在顶部:0 - Make Navbar stick at the top but when clicked on cross button in notification bar it should be removed and navbar stick to top:0 Bootstrap Navbar将品牌和Navbar-Right“粘贴”到网页的一侧 - Bootstrap Navbar Make Brand and Navbar-Right “Stick” to side of webpage 图像后如何使导航栏粘贴在页面顶部? - How to make navbar stick to the top of the page, after an image? 当我向上滚动时,导航栏必须停留在最高位置,并且不要通过跟随导航栏的图像来隐藏 - when i am scrolling up, the navbar has to stick at top position and don't hide by following images of navbar 如何根据用户的滚动方式将div粘贴到顶部或底部? - How to make a div stick to top or bottom, depending on how the user is scrolling? 滚动经过另一个 div 后,使 div 粘在页面顶部? - Make div stick to top of page after scrolling past another div? 滚动时尝试使导航菜单停留在屏幕顶部 - Trying to make nav menu stick at top of screen when scrolling
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM