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

后端 未结 3 1429
遇见更好的自我
遇见更好的自我 2021-01-05 21:25

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 a

相关标签:
3条回答
  • 2021-01-05 21:36

    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;).

    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>

    0 讨论(0)
  • 2021-01-05 21:55

    Without your javascript code, i can suggest you use position:sticky which achieves what you want.

    Read more here position CSS

    It is pretty well supported in modern browsers 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>

    0 讨论(0)
  • 2021-01-05 21:58

    Demo:

    http://jsfiddle.net/4tmcLjq3/3/

    Explanation:

    position: sticky with correct markup will do the work

    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. In my solution is stops before the next sticking.

    0 讨论(0)
提交回复
热议问题