title: Create smooth sticky header transition with CSS
body: <p>I created a sticky header for my page:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>$(document).ready(() =&gt; {
    initNavbarScroll();
  });
  
  function initNavbarScroll() {
    const navbar = $("#navbar");
    const navbarTopOffset = navbar.offset().top;
    const stickedCls = "navbarSticked";
  
    $(window).scroll(() =&gt; {
      const isSticked = navbar.hasClass(stickedCls);
      const windowScrolledDown = $(window).scrollTop() &gt; navbarTopOffset;
  
      if (windowScrolledDown) {
        if (!isSticked) {
          navbar.addClass(stickedCls);
        }
      } else {
        if (isSticked) {
          navbar.removeClass(stickedCls);
        }
      }
    });
  }</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>body {
    margin: 0;
  }
  
  #navbar {
    height: 60px;
    width: 100%;
    background: blue;
  }
  
  .navbarSticked {
    position: fixed;
    top: 0;
  }
  
  .box {
    height: 100px;
    border-bottom: 1px solid;
    background: red;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"&gt;&lt;/script&gt;
  &lt;div id="navbar"&gt;&lt;/div&gt;
  &lt;div class="box"&gt;&lt;/div&gt;
  &lt;div class="box"&gt;&lt;/div&gt;
  &lt;div class="box"&gt;&lt;/div&gt;
  &lt;div class="box"&gt;&lt;/div&gt;
  &lt;div class="box"&gt;&lt;/div&gt;
  &lt;div class="box"&gt;&lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>The header should be the same as on <a href="https://www.thenativeweb.io/" rel="nofollow noreferrer">this page</a>.</p>
  
  <p>As you can see in the code example above the transition of the header is not smooth it's a bit clunky.</p>
  
  <p>How can I make it stick smoothly?</p>