简体   繁体   中英

How can I fix the position of Sticky Header on Scroll?

In Angular application I am trying to shrink header component of Sticky header when user scrolls down. Currently sticky header works fine.

When user scroll down I am able to HIDE TEXT - Header and when user scroll up I can display the text.

The Problem is the header is not fixed when I scroll up. How can I make Header fixed to hide text and only display placeholder, dropdown and button without text?`

Here is the link to output which needs to be fixed- https://screenrec.com/share/G4bQcqhOgf

This is what I have tried so far

 .header-outer { display: flex; align-items: center; position: sticky; height: 120px; top: -52px } .header-container { width: 100%; height: 148px; background: grey; border-radius: 0px; top: 0; position: sticky; overflow: hidden; transition: 0.90s; }
 <header class="header-outer"> <div class="header-container"> <p>Header</p> <div class="placeholder"> <input type="search" /> </div> <div class="dropdowns"> <select> <option>Placeholder</option> </select> </div> <div class="btn"> <button> Search </button> </div> </div> </header>

You can add fixed-top class like this:

<header class="header-outer fixed-top">
  <div class="header-container">
    <p>Header</p>
    <div class="placeholder">
      <input type="search" />
    </div>
    <div class="dropdowns">
      <select>
        <option>Placeholder</option>
      </select>
    </div>
    <div class="btn">
      <button> Search </button>
    </div>
  </div>
</header>

You can use position: stricky , top: 0 to header

    <body>

  <header class="header-outer">
    <div class="header-container">
      <p>Header</p>
      <div class="placeholder">
        <input type="search" />
      </div>
      <div class="dropdowns">
        <select>
          <option>Placeholder</option>
        </select>
      </div>
      <div class="btn">
        <button> Search </button>
      </div>
    </div>
  </header>
  
  <div class="content">
  
  <p>

    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).


Where does it come from?
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.

  </p>
  </div>

  
</body>

.CSS

.header-container {
    background: grey;
    border-radius: 0px;
    padding: 10px
}

.header-container p{
       margin: 0;
}

header{
  position: sticky; 
  top: 0;
  width: 100%;
}

.content{
  height:500px;
  background: green
}

.content p{
  margin: 0;
}

body{
  margin:0px;
}

 .header-container { background: grey; border-radius: 0px; padding: 10px } .header-container p{ margin: 0; } header{ position: sticky; top: 0; width: 100%; } .content{ height:500px; background: green } .content p{ margin: 0; } body{ margin:0px; }
 <body> <header class="header-outer"> <div class="header-container"> <p>Header</p> <div class="placeholder"> <input type="search" /> </div> <div class="dropdowns"> <select> <option>Placeholder</option> </select> </div> <div class="btn"> <button> Search </button> </div> </div> </header> <div class="content"> <p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). Where does it come from? Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham. </p> </div> </body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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