滚动时 HTML 固定标题栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24878582/
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-08-29 02:18:16  来源:igfitidea点击:

HTML fixed header bar when scroll

htmlcsscss-position

提问by Eric Tseng

I've written an example here

我在这里写了一个例子

http://jsfiddle.net/R9Lds/

http://jsfiddle.net/R9Lds/

I want a header bar on the top of the page.

我想要页面顶部的标题栏。

<p style="vertical-align: middle; color: white">Head Bar</p>

And when user scrolls down and up, the header bar will always be on the top.

当用户向下和向上滚动时,标题栏将始终位于顶部。

But now it has an issue that the bottom of the page will be cut-off.

但是现在它有一个问题,即页面底部将被切断。

Just like the example above, the

就像上面的例子一样,

"Title 3",

“标题3”,

"Content 3.",

"内容 3.",

"Author: Alex"

“作者:亚历克斯”

will be cut-off.

将被切断。

Anyone has any ideas about this issue?

有人对这个问题有任何想法吗?

Thanks in advance.

提前致谢。

Eric

埃里克

回答by Dimas Pante

You actually can achieve it simpler than that. You only need to set your header div with position: fixed, and a 'push' div to, well... push the content down.

你实际上可以比这更简单地实现它。您只需要将标题 div 设置为position: fixed,然后将“推” div 设置为……将内容向下推。

Take a look in this Fiddle

看看这个小提琴

回答by Edward

You should set your header bar to position: fixedinstead.

您应该将标题栏设置为position: fixed

Example:

例子:

div.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
}

if you are concerned about the top of .home being cut off, you can also add a margin-top

如果您担心 .home 的顶部被切断,您还可以添加一个 margin-top

http://jsfiddle.net/R9Lds/1/

http://jsfiddle.net/R9Lds/1/