有人知道如何制作多个滚动固定的标题吗?我已经查看了像这样的答案。
我想要第一个标题,在已经固定在屏幕顶部的情况下,在第二个标题之前停止,并且当第一个标题被滚动超过时,第二个标题应该取代第一个标题,并粘在屏幕顶部。
但是,这样的答案对我不起作用,因为它们使用了我没有使用的库,例如jQuery,或者它们过于复杂。到目前为止,我已经通过getBoundingClientRect()
完成了工作,但只有两个标题。
我在这里提供了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>