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

MWR*_*MWR 6 html javascript css fixed onscroll

有谁知道如何在滚动固定标题上制作多个?我检查答案,如

这正是我想要的,但我希望该标题在另一个标题之前停止,并且当第一个标题滚动经过第二个标题时,第二个标题应该占据第一个标题的位置并停留在屏幕的最顶部。但是它们对我不起作用,因为它们使用的是我不使用的库(jQuery 等),而且它们过于复杂。我试过这样做,我getBoundingClientRect()只用 2 个标题就可以使用它。我在这里提供了 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;
    
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

Dev*_*h J 7

演示:

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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

在 jsFiddle 上查看

解释:

position: sticky使用正确的标记将完成工作

PS:我知道已经有一个答案使用,position: sticky但在该解决方案中,前一个标题不会停止,而是与下一个标题重叠。在我的解决方案中是在下一次粘贴之前停止。


mad*_*dav 6

如果您希望标头 2 和标头 3 具有粘性,但在下面显示下一个,top则为每个标头添加填充(此处标头 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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)