javascript 如何在滚动时使此导航栏浮动在顶部?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10197298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:00:20  来源:igfitidea点击:

How to make this navigation bar floating on top while scrolling?

javascriptjqueryhtmlcsscss-float

提问by nye17

The whole navigation bar along with its JavaScript settings can be found here

可以在此处找到整个导航栏及其 JavaScript 设置

http://jsfiddle.net/DyJzg/3/

http://jsfiddle.net/DyJzg/3/

right now it stays at the top of the page, but i am wonder whether it could be made floating on top even when you scroll down the page. Thanks.

现在它停留在页面的顶部,但我想知道即使你向下滚动页面,它是否也可以漂浮在顶部。谢谢。

回答by Stuart Burrows

I think you need to use position:fixed

我认为你需要使用 position:fixed

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

here is modified jsfiddle

这是修改后的 jsfiddle

回答by luchopintado

if your element is 'nav' or your element class is '.nav' use this piece of css code:

如果您的元素是 'nav' 或您的元素类是 '.nav' 使用这段 css 代码:

nav, .nav{
position:fixed;
top:0;
width:your_custom_with;
height: your_custom_height
}

note: check this article of css positioning for more details: http://www.w3schools.com/css/css_positioning.asp

注意:查看这篇css定位文章了解更多详情:http: //www.w3schools.com/css/css_positioning.asp

回答by Robin Maben

Just off the top of my head, the following should do it..

就在我的头顶上,以下应该做..

.nav{
   display:block;
   position:absolute;
   top:0;
   left:0;
   right:0;
   width:100%;
}