CSS Header effect (change background and color) when scrolling down










0















I am trying to do a nice effect when the header leaves the current block. I want the background color and the color of the text to change when scrolling down.



html:



<header class="green">Logo</header>
<header class="red">Logo</header>

<section id='content1'>
<h1>Content</h1>
<p>Goes here!</p>
</section>
<section id='content2'>
<h1>Content</h1>
<p>Goes here!</p>
</section>


css:



* 
box-sizing: border-box;
margin: 0;
padding: 0;


header
width: 100%;
position: fixed;
display: block;
top: 0;
font-size: 20pt;
padding: 15px 10px


.green
background: green;
color: #000;
z-index: 2


.red
background: red;
color: #fff;
z-index: 1


section
position: relative;
height: 500px;
padding: 100px 10px


#content1
background: #D9D9D9;
z-index: 1


#content2
background: #9FDAD0;
z-index: 2



An example serves best, something like this https://www.dropbox.com/



Thanks!










share|improve this question
























  • iám pretty sure they also used javascript to pull it off, so i've editted the tag javascript into the question.

    – Raymond Nijland
    Nov 15 '18 at 17:21












  • Check this it might help: stackoverflow.com/questions/44032900/…

    – Julian Espinosa
    Nov 15 '18 at 17:25











  • So onscroll check position and apply a class

    – epascarello
    Nov 15 '18 at 17:27











  • I dont want to change the color of the hole header. Want the header to be smooth and to fit the background 100%. I think this is possible with eg z-index and two headers like the code above, but I am missing something :P

    – LonniE
    Nov 15 '18 at 17:30











  • It seams W3C is working on it to make this part of css drafts.csswg.org/css-scroll-snap ... it's seams to be supported by Firefox and some other browser looking into this developer.mozilla.org/en-US/docs/Web/CSS/scroll-snap-type ... here is a "polyfill" hospodarets.com/css-scroll-snap

    – Raymond Nijland
    Nov 15 '18 at 17:30
















0















I am trying to do a nice effect when the header leaves the current block. I want the background color and the color of the text to change when scrolling down.



html:



<header class="green">Logo</header>
<header class="red">Logo</header>

<section id='content1'>
<h1>Content</h1>
<p>Goes here!</p>
</section>
<section id='content2'>
<h1>Content</h1>
<p>Goes here!</p>
</section>


css:



* 
box-sizing: border-box;
margin: 0;
padding: 0;


header
width: 100%;
position: fixed;
display: block;
top: 0;
font-size: 20pt;
padding: 15px 10px


.green
background: green;
color: #000;
z-index: 2


.red
background: red;
color: #fff;
z-index: 1


section
position: relative;
height: 500px;
padding: 100px 10px


#content1
background: #D9D9D9;
z-index: 1


#content2
background: #9FDAD0;
z-index: 2



An example serves best, something like this https://www.dropbox.com/



Thanks!










share|improve this question
























  • iám pretty sure they also used javascript to pull it off, so i've editted the tag javascript into the question.

    – Raymond Nijland
    Nov 15 '18 at 17:21












  • Check this it might help: stackoverflow.com/questions/44032900/…

    – Julian Espinosa
    Nov 15 '18 at 17:25











  • So onscroll check position and apply a class

    – epascarello
    Nov 15 '18 at 17:27











  • I dont want to change the color of the hole header. Want the header to be smooth and to fit the background 100%. I think this is possible with eg z-index and two headers like the code above, but I am missing something :P

    – LonniE
    Nov 15 '18 at 17:30











  • It seams W3C is working on it to make this part of css drafts.csswg.org/css-scroll-snap ... it's seams to be supported by Firefox and some other browser looking into this developer.mozilla.org/en-US/docs/Web/CSS/scroll-snap-type ... here is a "polyfill" hospodarets.com/css-scroll-snap

    – Raymond Nijland
    Nov 15 '18 at 17:30














0












0








0








I am trying to do a nice effect when the header leaves the current block. I want the background color and the color of the text to change when scrolling down.



html:



<header class="green">Logo</header>
<header class="red">Logo</header>

<section id='content1'>
<h1>Content</h1>
<p>Goes here!</p>
</section>
<section id='content2'>
<h1>Content</h1>
<p>Goes here!</p>
</section>


css:



* 
box-sizing: border-box;
margin: 0;
padding: 0;


header
width: 100%;
position: fixed;
display: block;
top: 0;
font-size: 20pt;
padding: 15px 10px


.green
background: green;
color: #000;
z-index: 2


.red
background: red;
color: #fff;
z-index: 1


section
position: relative;
height: 500px;
padding: 100px 10px


#content1
background: #D9D9D9;
z-index: 1


#content2
background: #9FDAD0;
z-index: 2



An example serves best, something like this https://www.dropbox.com/



Thanks!










share|improve this question
















I am trying to do a nice effect when the header leaves the current block. I want the background color and the color of the text to change when scrolling down.



html:



<header class="green">Logo</header>
<header class="red">Logo</header>

<section id='content1'>
<h1>Content</h1>
<p>Goes here!</p>
</section>
<section id='content2'>
<h1>Content</h1>
<p>Goes here!</p>
</section>


css:



* 
box-sizing: border-box;
margin: 0;
padding: 0;


header
width: 100%;
position: fixed;
display: block;
top: 0;
font-size: 20pt;
padding: 15px 10px


.green
background: green;
color: #000;
z-index: 2


.red
background: red;
color: #fff;
z-index: 1


section
position: relative;
height: 500px;
padding: 100px 10px


#content1
background: #D9D9D9;
z-index: 1


#content2
background: #9FDAD0;
z-index: 2



An example serves best, something like this https://www.dropbox.com/



Thanks!







javascript html css






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 17:21









Raymond Nijland

8,93621329




8,93621329










asked Nov 15 '18 at 17:17









LonniELonniE

646




646












  • iám pretty sure they also used javascript to pull it off, so i've editted the tag javascript into the question.

    – Raymond Nijland
    Nov 15 '18 at 17:21












  • Check this it might help: stackoverflow.com/questions/44032900/…

    – Julian Espinosa
    Nov 15 '18 at 17:25











  • So onscroll check position and apply a class

    – epascarello
    Nov 15 '18 at 17:27











  • I dont want to change the color of the hole header. Want the header to be smooth and to fit the background 100%. I think this is possible with eg z-index and two headers like the code above, but I am missing something :P

    – LonniE
    Nov 15 '18 at 17:30











  • It seams W3C is working on it to make this part of css drafts.csswg.org/css-scroll-snap ... it's seams to be supported by Firefox and some other browser looking into this developer.mozilla.org/en-US/docs/Web/CSS/scroll-snap-type ... here is a "polyfill" hospodarets.com/css-scroll-snap

    – Raymond Nijland
    Nov 15 '18 at 17:30


















  • iám pretty sure they also used javascript to pull it off, so i've editted the tag javascript into the question.

    – Raymond Nijland
    Nov 15 '18 at 17:21












  • Check this it might help: stackoverflow.com/questions/44032900/…

    – Julian Espinosa
    Nov 15 '18 at 17:25











  • So onscroll check position and apply a class

    – epascarello
    Nov 15 '18 at 17:27











  • I dont want to change the color of the hole header. Want the header to be smooth and to fit the background 100%. I think this is possible with eg z-index and two headers like the code above, but I am missing something :P

    – LonniE
    Nov 15 '18 at 17:30











  • It seams W3C is working on it to make this part of css drafts.csswg.org/css-scroll-snap ... it's seams to be supported by Firefox and some other browser looking into this developer.mozilla.org/en-US/docs/Web/CSS/scroll-snap-type ... here is a "polyfill" hospodarets.com/css-scroll-snap

    – Raymond Nijland
    Nov 15 '18 at 17:30

















iám pretty sure they also used javascript to pull it off, so i've editted the tag javascript into the question.

– Raymond Nijland
Nov 15 '18 at 17:21






iám pretty sure they also used javascript to pull it off, so i've editted the tag javascript into the question.

– Raymond Nijland
Nov 15 '18 at 17:21














Check this it might help: stackoverflow.com/questions/44032900/…

– Julian Espinosa
Nov 15 '18 at 17:25





Check this it might help: stackoverflow.com/questions/44032900/…

– Julian Espinosa
Nov 15 '18 at 17:25













So onscroll check position and apply a class

– epascarello
Nov 15 '18 at 17:27





So onscroll check position and apply a class

– epascarello
Nov 15 '18 at 17:27













I dont want to change the color of the hole header. Want the header to be smooth and to fit the background 100%. I think this is possible with eg z-index and two headers like the code above, but I am missing something :P

– LonniE
Nov 15 '18 at 17:30





I dont want to change the color of the hole header. Want the header to be smooth and to fit the background 100%. I think this is possible with eg z-index and two headers like the code above, but I am missing something :P

– LonniE
Nov 15 '18 at 17:30













It seams W3C is working on it to make this part of css drafts.csswg.org/css-scroll-snap ... it's seams to be supported by Firefox and some other browser looking into this developer.mozilla.org/en-US/docs/Web/CSS/scroll-snap-type ... here is a "polyfill" hospodarets.com/css-scroll-snap

– Raymond Nijland
Nov 15 '18 at 17:30






It seams W3C is working on it to make this part of css drafts.csswg.org/css-scroll-snap ... it's seams to be supported by Firefox and some other browser looking into this developer.mozilla.org/en-US/docs/Web/CSS/scroll-snap-type ... here is a "polyfill" hospodarets.com/css-scroll-snap

– Raymond Nijland
Nov 15 '18 at 17:30













3 Answers
3






active

oldest

votes


















1














So i redid it with some Javascript.
This effect is awesome, feel free to improve it if you like!
Is this possible to accomplish without Javascript?






const secondBlock = document.getElementById('content2')
const header = document.getElementsByTagName('header')
const headerHeight = 61

function setHeader ()
const scrollPoint = window.pageYOffset + headerHeight

let blockPoint = 61 - (scrollPoint - secondBlock.offsetTop)
if (blockPoint <= 0) blockPoint = 0

if (scrollPoint > secondBlock.offsetTop)
header[0].style = `max-height: $blockPointpx;`
else
header[0].style = `max-height: $headerHeightpx;`

window.requestAnimationFrame(setHeader)


window.requestAnimationFrame(setHeader)

* 
box-sizing: border-box;
margin: 0;
padding: 0;


header
display: block;
font-size: 20pt;
height: 61px;
line-height: 61px;
padding-left: 10px;
position: fixed;
top: 0;
width: 100%;


header#first
background: #8292C3;
color: #000;
overflow: hidden;
z-index: 10;


header#second
background: #82C3B9;
color: #fff;
z-index: 9;


section
height: 500px;
padding: 100px 10px;
position: relative;


#content1
background: #96A6D5;


#content2
background: #99D6CC;

<header id='first'>Logo - <small>scroll down to see the magic!</small></header>
<header id='second'>Logo - <small>scroll down to see the magic! COOL!</small></header>

<section id='content1'>
<h1>Content</h1>
<p>Goes here!</p>
</section>
<section id='content2'>
<h1>Content</h1>
<p>Goes here!</p>
</section>








share|improve this answer






























    0














    The smooth transition you're looking for can be done in CSS, but you'll need some JavaScript in order to initiate the animation.






    window.onscroll = function()
    var top = window.scrollY;
    console.log('Top: ' + top);
    var header = document.getElementsByTagName('header');
    var offset = header.innerHeight; //changed offset to be dynamic, so it works on mobile screens.
    if(top > offset)
    header[0].classList.remove('top');
    header[0].classList.add('scrolled');
    else
    header[0].classList.remove('scrolled');
    header[0].classList.add('top');

    ;

    * 
    box-sizing: border-box;
    margin: 0;
    padding: 0;

    body
    position: relative;

    header
    width: 100%;
    position: fixed;
    height: 75px;
    display: block;
    top: 0;
    z-index: 100;
    font-size: 20pt;
    padding: 15px 10px

    header.top
    background: #222;
    color: #fff;
    transition: all 0.3s ease;


    header.scrolled
    background: #555;
    color: #e0e0e0;
    transition: all 0.3s ease;


    .green
    background: green;
    color: #000;
    z-index: 2


    .red
    background: red;
    color: #fff;
    z-index: 1


    section
    position: relative;
    height: 800px;
    padding: 100px 10px


    #content1
    background: #D9D9D9;
    z-index: 1


    #content2
    background: #9FDAD0;
    z-index: 2

    	<header class="top">Logo</header>
    <section id='content1'>
    <h1>Content</h1>
    <p>Goes here!</p>
    </section>
    <section id='content2'>
    <h1>Content</h1>
    <p>Goes here!</p>
    </section>





    This basically says that when we scroll passed the height of the header (in this case 50px), remove the "top" class from it, and add the "scrolled" class from it. You can use the css selectors header.top and header.scrolled to do your transition animations. Be sure to use transition: background-color (time) (timing function), color (time) (timing function); to achieve the smoothness you're looking for.






    share|improve this answer

























    • Yes, this works but its not what I am after, look at dropbox.com header.

      – LonniE
      Nov 15 '18 at 17:41











    • Dropbox's header doesn't change colors, sizes, or anything. For this, all you need is the position:fixed; top:0 CSS call, and to make sure that your body is position: relative; so that the position of elements inside it work. EDIT: the only other thing I see is maybe the box shadow under the header, so it looks like it stands above the page? But otherwise I'm not sure I understand your question.

      – Ryan
      Nov 15 '18 at 17:42



















    0














    You can change it with the class. Like i use this website.



    http://www.moumaachi.com/
    it has class like this



     <div class="header-v2 navbar-fixed-top">


    but when scroll down to 50 px then it will showa this



    <div class="header-v2 navbar-fixed-top top-nav-collapse">


    and normal it has



    <div class="thiswillhide">


    but when scroll down more then it wil like this



    <div class="thiswillhide hidesearch">


    you can use this code



    <script>
    //jQuery to collapse the navbar on scroll
    $(window).scroll(function()
    if ($(".header-v2").offset().top > 50)
    $(".navbar-fixed-top").addClass("top-nav-collapse");
    else
    $(".navbar-fixed-top").removeClass("top-nav-collapse");

    if ($(".header-v2").offset().top > 600)
    $(".thiswillhide").addClass("hidesearch");
    else
    $(".thiswillhide").removeClass("hidesearch");

    );
    </script>


    thanks






    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324756%2fcss-header-effect-change-background-and-color-when-scrolling-down%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      So i redid it with some Javascript.
      This effect is awesome, feel free to improve it if you like!
      Is this possible to accomplish without Javascript?






      const secondBlock = document.getElementById('content2')
      const header = document.getElementsByTagName('header')
      const headerHeight = 61

      function setHeader ()
      const scrollPoint = window.pageYOffset + headerHeight

      let blockPoint = 61 - (scrollPoint - secondBlock.offsetTop)
      if (blockPoint <= 0) blockPoint = 0

      if (scrollPoint > secondBlock.offsetTop)
      header[0].style = `max-height: $blockPointpx;`
      else
      header[0].style = `max-height: $headerHeightpx;`

      window.requestAnimationFrame(setHeader)


      window.requestAnimationFrame(setHeader)

      * 
      box-sizing: border-box;
      margin: 0;
      padding: 0;


      header
      display: block;
      font-size: 20pt;
      height: 61px;
      line-height: 61px;
      padding-left: 10px;
      position: fixed;
      top: 0;
      width: 100%;


      header#first
      background: #8292C3;
      color: #000;
      overflow: hidden;
      z-index: 10;


      header#second
      background: #82C3B9;
      color: #fff;
      z-index: 9;


      section
      height: 500px;
      padding: 100px 10px;
      position: relative;


      #content1
      background: #96A6D5;


      #content2
      background: #99D6CC;

      <header id='first'>Logo - <small>scroll down to see the magic!</small></header>
      <header id='second'>Logo - <small>scroll down to see the magic! COOL!</small></header>

      <section id='content1'>
      <h1>Content</h1>
      <p>Goes here!</p>
      </section>
      <section id='content2'>
      <h1>Content</h1>
      <p>Goes here!</p>
      </section>








      share|improve this answer



























        1














        So i redid it with some Javascript.
        This effect is awesome, feel free to improve it if you like!
        Is this possible to accomplish without Javascript?






        const secondBlock = document.getElementById('content2')
        const header = document.getElementsByTagName('header')
        const headerHeight = 61

        function setHeader ()
        const scrollPoint = window.pageYOffset + headerHeight

        let blockPoint = 61 - (scrollPoint - secondBlock.offsetTop)
        if (blockPoint <= 0) blockPoint = 0

        if (scrollPoint > secondBlock.offsetTop)
        header[0].style = `max-height: $blockPointpx;`
        else
        header[0].style = `max-height: $headerHeightpx;`

        window.requestAnimationFrame(setHeader)


        window.requestAnimationFrame(setHeader)

        * 
        box-sizing: border-box;
        margin: 0;
        padding: 0;


        header
        display: block;
        font-size: 20pt;
        height: 61px;
        line-height: 61px;
        padding-left: 10px;
        position: fixed;
        top: 0;
        width: 100%;


        header#first
        background: #8292C3;
        color: #000;
        overflow: hidden;
        z-index: 10;


        header#second
        background: #82C3B9;
        color: #fff;
        z-index: 9;


        section
        height: 500px;
        padding: 100px 10px;
        position: relative;


        #content1
        background: #96A6D5;


        #content2
        background: #99D6CC;

        <header id='first'>Logo - <small>scroll down to see the magic!</small></header>
        <header id='second'>Logo - <small>scroll down to see the magic! COOL!</small></header>

        <section id='content1'>
        <h1>Content</h1>
        <p>Goes here!</p>
        </section>
        <section id='content2'>
        <h1>Content</h1>
        <p>Goes here!</p>
        </section>








        share|improve this answer

























          1












          1








          1







          So i redid it with some Javascript.
          This effect is awesome, feel free to improve it if you like!
          Is this possible to accomplish without Javascript?






          const secondBlock = document.getElementById('content2')
          const header = document.getElementsByTagName('header')
          const headerHeight = 61

          function setHeader ()
          const scrollPoint = window.pageYOffset + headerHeight

          let blockPoint = 61 - (scrollPoint - secondBlock.offsetTop)
          if (blockPoint <= 0) blockPoint = 0

          if (scrollPoint > secondBlock.offsetTop)
          header[0].style = `max-height: $blockPointpx;`
          else
          header[0].style = `max-height: $headerHeightpx;`

          window.requestAnimationFrame(setHeader)


          window.requestAnimationFrame(setHeader)

          * 
          box-sizing: border-box;
          margin: 0;
          padding: 0;


          header
          display: block;
          font-size: 20pt;
          height: 61px;
          line-height: 61px;
          padding-left: 10px;
          position: fixed;
          top: 0;
          width: 100%;


          header#first
          background: #8292C3;
          color: #000;
          overflow: hidden;
          z-index: 10;


          header#second
          background: #82C3B9;
          color: #fff;
          z-index: 9;


          section
          height: 500px;
          padding: 100px 10px;
          position: relative;


          #content1
          background: #96A6D5;


          #content2
          background: #99D6CC;

          <header id='first'>Logo - <small>scroll down to see the magic!</small></header>
          <header id='second'>Logo - <small>scroll down to see the magic! COOL!</small></header>

          <section id='content1'>
          <h1>Content</h1>
          <p>Goes here!</p>
          </section>
          <section id='content2'>
          <h1>Content</h1>
          <p>Goes here!</p>
          </section>








          share|improve this answer













          So i redid it with some Javascript.
          This effect is awesome, feel free to improve it if you like!
          Is this possible to accomplish without Javascript?






          const secondBlock = document.getElementById('content2')
          const header = document.getElementsByTagName('header')
          const headerHeight = 61

          function setHeader ()
          const scrollPoint = window.pageYOffset + headerHeight

          let blockPoint = 61 - (scrollPoint - secondBlock.offsetTop)
          if (blockPoint <= 0) blockPoint = 0

          if (scrollPoint > secondBlock.offsetTop)
          header[0].style = `max-height: $blockPointpx;`
          else
          header[0].style = `max-height: $headerHeightpx;`

          window.requestAnimationFrame(setHeader)


          window.requestAnimationFrame(setHeader)

          * 
          box-sizing: border-box;
          margin: 0;
          padding: 0;


          header
          display: block;
          font-size: 20pt;
          height: 61px;
          line-height: 61px;
          padding-left: 10px;
          position: fixed;
          top: 0;
          width: 100%;


          header#first
          background: #8292C3;
          color: #000;
          overflow: hidden;
          z-index: 10;


          header#second
          background: #82C3B9;
          color: #fff;
          z-index: 9;


          section
          height: 500px;
          padding: 100px 10px;
          position: relative;


          #content1
          background: #96A6D5;


          #content2
          background: #99D6CC;

          <header id='first'>Logo - <small>scroll down to see the magic!</small></header>
          <header id='second'>Logo - <small>scroll down to see the magic! COOL!</small></header>

          <section id='content1'>
          <h1>Content</h1>
          <p>Goes here!</p>
          </section>
          <section id='content2'>
          <h1>Content</h1>
          <p>Goes here!</p>
          </section>








          const secondBlock = document.getElementById('content2')
          const header = document.getElementsByTagName('header')
          const headerHeight = 61

          function setHeader ()
          const scrollPoint = window.pageYOffset + headerHeight

          let blockPoint = 61 - (scrollPoint - secondBlock.offsetTop)
          if (blockPoint <= 0) blockPoint = 0

          if (scrollPoint > secondBlock.offsetTop)
          header[0].style = `max-height: $blockPointpx;`
          else
          header[0].style = `max-height: $headerHeightpx;`

          window.requestAnimationFrame(setHeader)


          window.requestAnimationFrame(setHeader)

          * 
          box-sizing: border-box;
          margin: 0;
          padding: 0;


          header
          display: block;
          font-size: 20pt;
          height: 61px;
          line-height: 61px;
          padding-left: 10px;
          position: fixed;
          top: 0;
          width: 100%;


          header#first
          background: #8292C3;
          color: #000;
          overflow: hidden;
          z-index: 10;


          header#second
          background: #82C3B9;
          color: #fff;
          z-index: 9;


          section
          height: 500px;
          padding: 100px 10px;
          position: relative;


          #content1
          background: #96A6D5;


          #content2
          background: #99D6CC;

          <header id='first'>Logo - <small>scroll down to see the magic!</small></header>
          <header id='second'>Logo - <small>scroll down to see the magic! COOL!</small></header>

          <section id='content1'>
          <h1>Content</h1>
          <p>Goes here!</p>
          </section>
          <section id='content2'>
          <h1>Content</h1>
          <p>Goes here!</p>
          </section>





          const secondBlock = document.getElementById('content2')
          const header = document.getElementsByTagName('header')
          const headerHeight = 61

          function setHeader ()
          const scrollPoint = window.pageYOffset + headerHeight

          let blockPoint = 61 - (scrollPoint - secondBlock.offsetTop)
          if (blockPoint <= 0) blockPoint = 0

          if (scrollPoint > secondBlock.offsetTop)
          header[0].style = `max-height: $blockPointpx;`
          else
          header[0].style = `max-height: $headerHeightpx;`

          window.requestAnimationFrame(setHeader)


          window.requestAnimationFrame(setHeader)

          * 
          box-sizing: border-box;
          margin: 0;
          padding: 0;


          header
          display: block;
          font-size: 20pt;
          height: 61px;
          line-height: 61px;
          padding-left: 10px;
          position: fixed;
          top: 0;
          width: 100%;


          header#first
          background: #8292C3;
          color: #000;
          overflow: hidden;
          z-index: 10;


          header#second
          background: #82C3B9;
          color: #fff;
          z-index: 9;


          section
          height: 500px;
          padding: 100px 10px;
          position: relative;


          #content1
          background: #96A6D5;


          #content2
          background: #99D6CC;

          <header id='first'>Logo - <small>scroll down to see the magic!</small></header>
          <header id='second'>Logo - <small>scroll down to see the magic! COOL!</small></header>

          <section id='content1'>
          <h1>Content</h1>
          <p>Goes here!</p>
          </section>
          <section id='content2'>
          <h1>Content</h1>
          <p>Goes here!</p>
          </section>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 19:41









          LonniELonniE

          646




          646























              0














              The smooth transition you're looking for can be done in CSS, but you'll need some JavaScript in order to initiate the animation.






              window.onscroll = function()
              var top = window.scrollY;
              console.log('Top: ' + top);
              var header = document.getElementsByTagName('header');
              var offset = header.innerHeight; //changed offset to be dynamic, so it works on mobile screens.
              if(top > offset)
              header[0].classList.remove('top');
              header[0].classList.add('scrolled');
              else
              header[0].classList.remove('scrolled');
              header[0].classList.add('top');

              ;

              * 
              box-sizing: border-box;
              margin: 0;
              padding: 0;

              body
              position: relative;

              header
              width: 100%;
              position: fixed;
              height: 75px;
              display: block;
              top: 0;
              z-index: 100;
              font-size: 20pt;
              padding: 15px 10px

              header.top
              background: #222;
              color: #fff;
              transition: all 0.3s ease;


              header.scrolled
              background: #555;
              color: #e0e0e0;
              transition: all 0.3s ease;


              .green
              background: green;
              color: #000;
              z-index: 2


              .red
              background: red;
              color: #fff;
              z-index: 1


              section
              position: relative;
              height: 800px;
              padding: 100px 10px


              #content1
              background: #D9D9D9;
              z-index: 1


              #content2
              background: #9FDAD0;
              z-index: 2

              	<header class="top">Logo</header>
              <section id='content1'>
              <h1>Content</h1>
              <p>Goes here!</p>
              </section>
              <section id='content2'>
              <h1>Content</h1>
              <p>Goes here!</p>
              </section>





              This basically says that when we scroll passed the height of the header (in this case 50px), remove the "top" class from it, and add the "scrolled" class from it. You can use the css selectors header.top and header.scrolled to do your transition animations. Be sure to use transition: background-color (time) (timing function), color (time) (timing function); to achieve the smoothness you're looking for.






              share|improve this answer

























              • Yes, this works but its not what I am after, look at dropbox.com header.

                – LonniE
                Nov 15 '18 at 17:41











              • Dropbox's header doesn't change colors, sizes, or anything. For this, all you need is the position:fixed; top:0 CSS call, and to make sure that your body is position: relative; so that the position of elements inside it work. EDIT: the only other thing I see is maybe the box shadow under the header, so it looks like it stands above the page? But otherwise I'm not sure I understand your question.

                – Ryan
                Nov 15 '18 at 17:42
















              0














              The smooth transition you're looking for can be done in CSS, but you'll need some JavaScript in order to initiate the animation.






              window.onscroll = function()
              var top = window.scrollY;
              console.log('Top: ' + top);
              var header = document.getElementsByTagName('header');
              var offset = header.innerHeight; //changed offset to be dynamic, so it works on mobile screens.
              if(top > offset)
              header[0].classList.remove('top');
              header[0].classList.add('scrolled');
              else
              header[0].classList.remove('scrolled');
              header[0].classList.add('top');

              ;

              * 
              box-sizing: border-box;
              margin: 0;
              padding: 0;

              body
              position: relative;

              header
              width: 100%;
              position: fixed;
              height: 75px;
              display: block;
              top: 0;
              z-index: 100;
              font-size: 20pt;
              padding: 15px 10px

              header.top
              background: #222;
              color: #fff;
              transition: all 0.3s ease;


              header.scrolled
              background: #555;
              color: #e0e0e0;
              transition: all 0.3s ease;


              .green
              background: green;
              color: #000;
              z-index: 2


              .red
              background: red;
              color: #fff;
              z-index: 1


              section
              position: relative;
              height: 800px;
              padding: 100px 10px


              #content1
              background: #D9D9D9;
              z-index: 1


              #content2
              background: #9FDAD0;
              z-index: 2

              	<header class="top">Logo</header>
              <section id='content1'>
              <h1>Content</h1>
              <p>Goes here!</p>
              </section>
              <section id='content2'>
              <h1>Content</h1>
              <p>Goes here!</p>
              </section>





              This basically says that when we scroll passed the height of the header (in this case 50px), remove the "top" class from it, and add the "scrolled" class from it. You can use the css selectors header.top and header.scrolled to do your transition animations. Be sure to use transition: background-color (time) (timing function), color (time) (timing function); to achieve the smoothness you're looking for.






              share|improve this answer

























              • Yes, this works but its not what I am after, look at dropbox.com header.

                – LonniE
                Nov 15 '18 at 17:41











              • Dropbox's header doesn't change colors, sizes, or anything. For this, all you need is the position:fixed; top:0 CSS call, and to make sure that your body is position: relative; so that the position of elements inside it work. EDIT: the only other thing I see is maybe the box shadow under the header, so it looks like it stands above the page? But otherwise I'm not sure I understand your question.

                – Ryan
                Nov 15 '18 at 17:42














              0












              0








              0







              The smooth transition you're looking for can be done in CSS, but you'll need some JavaScript in order to initiate the animation.






              window.onscroll = function()
              var top = window.scrollY;
              console.log('Top: ' + top);
              var header = document.getElementsByTagName('header');
              var offset = header.innerHeight; //changed offset to be dynamic, so it works on mobile screens.
              if(top > offset)
              header[0].classList.remove('top');
              header[0].classList.add('scrolled');
              else
              header[0].classList.remove('scrolled');
              header[0].classList.add('top');

              ;

              * 
              box-sizing: border-box;
              margin: 0;
              padding: 0;

              body
              position: relative;

              header
              width: 100%;
              position: fixed;
              height: 75px;
              display: block;
              top: 0;
              z-index: 100;
              font-size: 20pt;
              padding: 15px 10px

              header.top
              background: #222;
              color: #fff;
              transition: all 0.3s ease;


              header.scrolled
              background: #555;
              color: #e0e0e0;
              transition: all 0.3s ease;


              .green
              background: green;
              color: #000;
              z-index: 2


              .red
              background: red;
              color: #fff;
              z-index: 1


              section
              position: relative;
              height: 800px;
              padding: 100px 10px


              #content1
              background: #D9D9D9;
              z-index: 1


              #content2
              background: #9FDAD0;
              z-index: 2

              	<header class="top">Logo</header>
              <section id='content1'>
              <h1>Content</h1>
              <p>Goes here!</p>
              </section>
              <section id='content2'>
              <h1>Content</h1>
              <p>Goes here!</p>
              </section>





              This basically says that when we scroll passed the height of the header (in this case 50px), remove the "top" class from it, and add the "scrolled" class from it. You can use the css selectors header.top and header.scrolled to do your transition animations. Be sure to use transition: background-color (time) (timing function), color (time) (timing function); to achieve the smoothness you're looking for.






              share|improve this answer















              The smooth transition you're looking for can be done in CSS, but you'll need some JavaScript in order to initiate the animation.






              window.onscroll = function()
              var top = window.scrollY;
              console.log('Top: ' + top);
              var header = document.getElementsByTagName('header');
              var offset = header.innerHeight; //changed offset to be dynamic, so it works on mobile screens.
              if(top > offset)
              header[0].classList.remove('top');
              header[0].classList.add('scrolled');
              else
              header[0].classList.remove('scrolled');
              header[0].classList.add('top');

              ;

              * 
              box-sizing: border-box;
              margin: 0;
              padding: 0;

              body
              position: relative;

              header
              width: 100%;
              position: fixed;
              height: 75px;
              display: block;
              top: 0;
              z-index: 100;
              font-size: 20pt;
              padding: 15px 10px

              header.top
              background: #222;
              color: #fff;
              transition: all 0.3s ease;


              header.scrolled
              background: #555;
              color: #e0e0e0;
              transition: all 0.3s ease;


              .green
              background: green;
              color: #000;
              z-index: 2


              .red
              background: red;
              color: #fff;
              z-index: 1


              section
              position: relative;
              height: 800px;
              padding: 100px 10px


              #content1
              background: #D9D9D9;
              z-index: 1


              #content2
              background: #9FDAD0;
              z-index: 2

              	<header class="top">Logo</header>
              <section id='content1'>
              <h1>Content</h1>
              <p>Goes here!</p>
              </section>
              <section id='content2'>
              <h1>Content</h1>
              <p>Goes here!</p>
              </section>





              This basically says that when we scroll passed the height of the header (in this case 50px), remove the "top" class from it, and add the "scrolled" class from it. You can use the css selectors header.top and header.scrolled to do your transition animations. Be sure to use transition: background-color (time) (timing function), color (time) (timing function); to achieve the smoothness you're looking for.






              window.onscroll = function()
              var top = window.scrollY;
              console.log('Top: ' + top);
              var header = document.getElementsByTagName('header');
              var offset = header.innerHeight; //changed offset to be dynamic, so it works on mobile screens.
              if(top > offset)
              header[0].classList.remove('top');
              header[0].classList.add('scrolled');
              else
              header[0].classList.remove('scrolled');
              header[0].classList.add('top');

              ;

              * 
              box-sizing: border-box;
              margin: 0;
              padding: 0;

              body
              position: relative;

              header
              width: 100%;
              position: fixed;
              height: 75px;
              display: block;
              top: 0;
              z-index: 100;
              font-size: 20pt;
              padding: 15px 10px

              header.top
              background: #222;
              color: #fff;
              transition: all 0.3s ease;


              header.scrolled
              background: #555;
              color: #e0e0e0;
              transition: all 0.3s ease;


              .green
              background: green;
              color: #000;
              z-index: 2


              .red
              background: red;
              color: #fff;
              z-index: 1


              section
              position: relative;
              height: 800px;
              padding: 100px 10px


              #content1
              background: #D9D9D9;
              z-index: 1


              #content2
              background: #9FDAD0;
              z-index: 2

              	<header class="top">Logo</header>
              <section id='content1'>
              <h1>Content</h1>
              <p>Goes here!</p>
              </section>
              <section id='content2'>
              <h1>Content</h1>
              <p>Goes here!</p>
              </section>





              window.onscroll = function()
              var top = window.scrollY;
              console.log('Top: ' + top);
              var header = document.getElementsByTagName('header');
              var offset = header.innerHeight; //changed offset to be dynamic, so it works on mobile screens.
              if(top > offset)
              header[0].classList.remove('top');
              header[0].classList.add('scrolled');
              else
              header[0].classList.remove('scrolled');
              header[0].classList.add('top');

              ;

              * 
              box-sizing: border-box;
              margin: 0;
              padding: 0;

              body
              position: relative;

              header
              width: 100%;
              position: fixed;
              height: 75px;
              display: block;
              top: 0;
              z-index: 100;
              font-size: 20pt;
              padding: 15px 10px

              header.top
              background: #222;
              color: #fff;
              transition: all 0.3s ease;


              header.scrolled
              background: #555;
              color: #e0e0e0;
              transition: all 0.3s ease;


              .green
              background: green;
              color: #000;
              z-index: 2


              .red
              background: red;
              color: #fff;
              z-index: 1


              section
              position: relative;
              height: 800px;
              padding: 100px 10px


              #content1
              background: #D9D9D9;
              z-index: 1


              #content2
              background: #9FDAD0;
              z-index: 2

              	<header class="top">Logo</header>
              <section id='content1'>
              <h1>Content</h1>
              <p>Goes here!</p>
              </section>
              <section id='content2'>
              <h1>Content</h1>
              <p>Goes here!</p>
              </section>






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 15 '18 at 17:44

























              answered Nov 15 '18 at 17:38









              RyanRyan

              31619




              31619












              • Yes, this works but its not what I am after, look at dropbox.com header.

                – LonniE
                Nov 15 '18 at 17:41











              • Dropbox's header doesn't change colors, sizes, or anything. For this, all you need is the position:fixed; top:0 CSS call, and to make sure that your body is position: relative; so that the position of elements inside it work. EDIT: the only other thing I see is maybe the box shadow under the header, so it looks like it stands above the page? But otherwise I'm not sure I understand your question.

                – Ryan
                Nov 15 '18 at 17:42


















              • Yes, this works but its not what I am after, look at dropbox.com header.

                – LonniE
                Nov 15 '18 at 17:41











              • Dropbox's header doesn't change colors, sizes, or anything. For this, all you need is the position:fixed; top:0 CSS call, and to make sure that your body is position: relative; so that the position of elements inside it work. EDIT: the only other thing I see is maybe the box shadow under the header, so it looks like it stands above the page? But otherwise I'm not sure I understand your question.

                – Ryan
                Nov 15 '18 at 17:42

















              Yes, this works but its not what I am after, look at dropbox.com header.

              – LonniE
              Nov 15 '18 at 17:41





              Yes, this works but its not what I am after, look at dropbox.com header.

              – LonniE
              Nov 15 '18 at 17:41













              Dropbox's header doesn't change colors, sizes, or anything. For this, all you need is the position:fixed; top:0 CSS call, and to make sure that your body is position: relative; so that the position of elements inside it work. EDIT: the only other thing I see is maybe the box shadow under the header, so it looks like it stands above the page? But otherwise I'm not sure I understand your question.

              – Ryan
              Nov 15 '18 at 17:42






              Dropbox's header doesn't change colors, sizes, or anything. For this, all you need is the position:fixed; top:0 CSS call, and to make sure that your body is position: relative; so that the position of elements inside it work. EDIT: the only other thing I see is maybe the box shadow under the header, so it looks like it stands above the page? But otherwise I'm not sure I understand your question.

              – Ryan
              Nov 15 '18 at 17:42












              0














              You can change it with the class. Like i use this website.



              http://www.moumaachi.com/
              it has class like this



               <div class="header-v2 navbar-fixed-top">


              but when scroll down to 50 px then it will showa this



              <div class="header-v2 navbar-fixed-top top-nav-collapse">


              and normal it has



              <div class="thiswillhide">


              but when scroll down more then it wil like this



              <div class="thiswillhide hidesearch">


              you can use this code



              <script>
              //jQuery to collapse the navbar on scroll
              $(window).scroll(function()
              if ($(".header-v2").offset().top > 50)
              $(".navbar-fixed-top").addClass("top-nav-collapse");
              else
              $(".navbar-fixed-top").removeClass("top-nav-collapse");

              if ($(".header-v2").offset().top > 600)
              $(".thiswillhide").addClass("hidesearch");
              else
              $(".thiswillhide").removeClass("hidesearch");

              );
              </script>


              thanks






              share|improve this answer



























                0














                You can change it with the class. Like i use this website.



                http://www.moumaachi.com/
                it has class like this



                 <div class="header-v2 navbar-fixed-top">


                but when scroll down to 50 px then it will showa this



                <div class="header-v2 navbar-fixed-top top-nav-collapse">


                and normal it has



                <div class="thiswillhide">


                but when scroll down more then it wil like this



                <div class="thiswillhide hidesearch">


                you can use this code



                <script>
                //jQuery to collapse the navbar on scroll
                $(window).scroll(function()
                if ($(".header-v2").offset().top > 50)
                $(".navbar-fixed-top").addClass("top-nav-collapse");
                else
                $(".navbar-fixed-top").removeClass("top-nav-collapse");

                if ($(".header-v2").offset().top > 600)
                $(".thiswillhide").addClass("hidesearch");
                else
                $(".thiswillhide").removeClass("hidesearch");

                );
                </script>


                thanks






                share|improve this answer

























                  0












                  0








                  0







                  You can change it with the class. Like i use this website.



                  http://www.moumaachi.com/
                  it has class like this



                   <div class="header-v2 navbar-fixed-top">


                  but when scroll down to 50 px then it will showa this



                  <div class="header-v2 navbar-fixed-top top-nav-collapse">


                  and normal it has



                  <div class="thiswillhide">


                  but when scroll down more then it wil like this



                  <div class="thiswillhide hidesearch">


                  you can use this code



                  <script>
                  //jQuery to collapse the navbar on scroll
                  $(window).scroll(function()
                  if ($(".header-v2").offset().top > 50)
                  $(".navbar-fixed-top").addClass("top-nav-collapse");
                  else
                  $(".navbar-fixed-top").removeClass("top-nav-collapse");

                  if ($(".header-v2").offset().top > 600)
                  $(".thiswillhide").addClass("hidesearch");
                  else
                  $(".thiswillhide").removeClass("hidesearch");

                  );
                  </script>


                  thanks






                  share|improve this answer













                  You can change it with the class. Like i use this website.



                  http://www.moumaachi.com/
                  it has class like this



                   <div class="header-v2 navbar-fixed-top">


                  but when scroll down to 50 px then it will showa this



                  <div class="header-v2 navbar-fixed-top top-nav-collapse">


                  and normal it has



                  <div class="thiswillhide">


                  but when scroll down more then it wil like this



                  <div class="thiswillhide hidesearch">


                  you can use this code



                  <script>
                  //jQuery to collapse the navbar on scroll
                  $(window).scroll(function()
                  if ($(".header-v2").offset().top > 50)
                  $(".navbar-fixed-top").addClass("top-nav-collapse");
                  else
                  $(".navbar-fixed-top").removeClass("top-nav-collapse");

                  if ($(".header-v2").offset().top > 600)
                  $(".thiswillhide").addClass("hidesearch");
                  else
                  $(".thiswillhide").removeClass("hidesearch");

                  );
                  </script>


                  thanks







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 17:58









                  Amranur RahmanAmranur Rahman

                  490815




                  490815



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324756%2fcss-header-effect-change-background-and-color-when-scrolling-down%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3

                      Museum of Modern and Contemporary Art of Trento and Rovereto