简体   繁体   English

如何在滚动固定标题/导航栏上制作多个贴在页面顶部的固定标题/导航栏?

[英]How can I make multiple on scroll fixed headers/navbars that stick at the very top of the page?

Does anyone know how to make multiple on scroll fixed headers?有谁知道如何在滚动固定标题上制作多个? I've checked answers such as this .我检查答案,如

It's kind of what I want, but I want that header to stop before another header and when the first header gets scrolled past the second header should be taking the first header's place and stick at the very top of the screen.这是我想要的,但我希望该标题在另一个标题之前停止,并且当第一个标题滚动经过第二个标题时,应该占据第一个标题的位置并粘在屏幕的最顶部。 But they don't work for me because they're using libraries that I'm not working with(jQuery, etc.) and they are overly, overly complicated.但它们对我不起作用,因为它们使用的是我不使用的库(jQuery 等),而且它们过于复杂。 I've tried to do this, I got it to work with getBoundingClientRect() with only 2 headers.我试过这样做,我让它与只有 2 个标题的getBoundingClientRect()一起工作。 I've provided the HTML&CSS part here:我在这里提供了 HTML&CSS 部分:

 html, body { margin: 0; padding: 0; width: 100%; height: 100%; font-family: 'Roboto', sans-serif; } @import url("https://fonts.googleapis.com/css?family=Roboto:100"); h1 { letter-spacing: 3px; margin: 0; padding-left: 10px; padding-top: 10px; color: #fff; font-weight: 100; } .header { width: 100%; height: 50px; } .header:nth-of-type(1){ background-color: dodgerblue; position: fixed; } .header:nth-of-type(2){ background-color: rebeccapurple; } .header:nth-of-type(3){ background-color: chartreuse; } .content { width: 100%; height: 100%; background: linear-gradient(70deg, orange, crimson); padding-top: 50px; }
 <header class="header"><h1>HEADER 1</h1></header> <div class="content"><h1>CONTENT</h1></div> <header class="header"><h1>HEADER 2</h1></header> <div class="content"><h1>CONTENT</h1></div> <header class="header"><h1>HEADER 3</h1></header> <div class="content"><h1>CONTENT</h1></div>

Demo:演示:

 html, body { margin: 0; padding: 0; width: 100%; height: 100%; font-family: 'Roboto', sans-serif; } @import url("https://fonts.googleapis.com/css?family=Roboto:100"); h1 { letter-spacing: 3px; margin: 0; padding-left: 10px; padding-top: 10px; color: #fff; font-weight: 100; } .content { width: 100%; height: 100%; background: linear-gradient(70deg, orange, crimson); } .content .header { width: 100%; height: 50px; position: sticky; top: 0; } .content:nth-of-type(1) .header { background-color: dodgerblue; } .content:nth-of-type(2) .header { background-color: rebeccapurple; } .content:nth-of-type(3) .header { background-color: chartreuse; }
 <div class="content"> <header class="header"> <h1>HEADER 1</h1> </header> <div class="content-inner"> <h1>CONTENT</h1> </div> </div> <div class="content"> <header class="header"> <h1>HEADER 2</h1> </header> <div class="content-inner"> <h1>CONTENT</h1> </div> </div> <div class="content"> <header class="header"> <h1>HEADER 3</h1> </header> .content <h1>CONTENT</h1> </div>

View on jsFiddle在 jsFiddle 上查看

Explanation:解释:

position: sticky with correct markup will do the work position: sticky正确的标记将完成工作

PS: I know there is already an answer using position: sticky but in that solution the previous header doesn't stop but overlaps with the next one. PS:我知道已经有一个使用position: sticky的答案,但在该解决方案中,前一个标题不会停止,而是与下一个重叠。 In my solution is stops before the next sticking.在我的解决方案中是在下一次粘贴之前停止。

Without your javascript code, i can suggest you use position:sticky which achieves what you want.如果没有你的 javascript 代码,我可以建议你使用position:sticky来实现你想要的。

Read more here position CSS在此处阅读更多位置 CSS

It is pretty well supported in modern browsers caniuse position sticky它在现代浏览器中得到很好的支持caniuse position sticky

 html, body { margin: 0; padding: 0; width: 100%; height: 100%; font-family: 'Roboto', sans-serif; position:relative; } @import url("https://fonts.googleapis.com/css?family=Roboto:100"); h1 { letter-spacing: 3px; margin: 0; padding-left: 10px; padding-top: 10px; color: #fff; font-weight: 100; } .header { width: 100%; height: 50px; position: sticky; top: 0px; } .header:nth-of-type(1){ background-color: dodgerblue; } .header:nth-of-type(2){ background-color: rebeccapurple; } .header:nth-of-type(3){ background-color: chartreuse; } .content { width: 100vw; height: 100vh; background: linear-gradient(70deg, orange, crimson); padding-top: 50px; }
 <section> <header class="header"><h1>HEADER 1</h1></header> <div class="content"><h1>CONTENT</h1></div> <header class="header"><h1>HEADER 2</h1></header> <div class="content"><h1>CONTENT</h1></div> <header class="header"><h1>HEADER 3</h1></header> <div class="content"><h1>CONTENT</h1></div> </section>

If you want header 2 and header 3 to be sticky, but show below the next add top with padding to each header (here header-2 has top: 50px; so it will not override the first, and the third has top: 100px; ).如果您希望标题 2 和标题 3 具有粘性,但在下一个下方显示为每个标题添加带有填充的top (此处 header-2 具有top: 50px;因此它不会覆盖第一个,而第三个具有top: 100px; )。

 html, body { margin: 0; padding: 0; width: 100%; height: 100%; font-family: 'Roboto', sans-serif; position:relative; } @import url("https://fonts.googleapis.com/css?family=Roboto:100"); h1 { letter-spacing: 3px; margin: 0; padding-left: 10px; padding-top: 10px; color: #fff; font-weight: 100; } .header { width: 100%; height: 50px; position: sticky; top: 0px; } .header:nth-of-type(1){ background-color: dodgerblue; } .header:nth-of-type(2){ background-color: rebeccapurple; } .header:nth-of-type(3){ background-color: chartreuse; } .content { width: 100vw; height: 100vh; background: linear-gradient(70deg, orange, crimson); padding-top: 50px; } .header-2{ top: 50px; } .header-3{ top: 100px; }
 <section> <header class="header"><h1>HEADER 1</h1></header> <div class="content"><h1>CONTENT</h1></div> <header class="header header-2"><h1>HEADER 2</h1></header> <div class="content"><h1>CONTENT</h1></div> <header class="header header-3"><h1>HEADER 3</h1></header> <div class="content"><h1>CONTENT</h1></div> </section>

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

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