title: Hiding top header on scroll
body: <p>I know this question has been asked, but I am having a bug and I cannot find the mistake.</p>
  
  <p>I have a top header above the navbar and when user scrolls down the height of the header (it is 50px) then I need to hide it. It works perfectly fine on my browser when I use the developer tools device toolbar, but after uploading to server I get a following bug: when I first scroll, the header is hidden, but when I scroll for the second time, it shows again and when I scroll for the third time, it disappears completely and it works. What could be the issue?</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(function() {
  
    //hide the top logo
    var wdwWidth = $(window).width();
    if (wdwWidth &lt;= 768) {
      $(".navbar").css("top", 50);
      var stickyHeaderTop = $('.navbar').offset().top;
      if ($(window).scrollTop() &gt; stickyHeaderTop) {
        $(".navbar").css("top", "0px");
      } else {
        $(".navbar").css("top", "50px");
      }
      $(window).scroll(function() {
        if ($(this).scrollTop() &gt;= stickyHeaderTop) {
          var scroll = $(window).scrollTop();
          var navTop = 50 - scroll;
          if (navTop &lt;= 0) {
            navTop = 0;
          }
          $(".navbar").css("top", navTop);
        } else {
          $(".navbar").css("top", "50px");
        }
      });
    } else {
      $(".navbar").css("top", "0px");
    }
  });</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>html {
    position: relative;
    min-height: 100%;
    height: 900px;
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased !important;
  }
  
  body {
    overflow-x: hidden;
    margin: 0 0 164px;
  }
  
  
  
  /*******************************
  MENU AND HEADER
  *******************************/
  
  #header-top-logo {
    display: none;
    height: 50px;
    background-color: rgba(167, 85, 194, 0.9);
    text-align: center;
    position: fixed;
    left: 0;
    width: 105%;
  }
  
  .super {
    font-family: Norican;
    font-size: 28px;
    font-weight: normal;
    font-style: normal;
    font-stretch: normal;
    text-align: center;
    color: #ffea6a;
  }
  
  .hoidjad {
    font-family: LeagueSpartan;
    font-size: 19px;
    font-weight: normal;
    font-style: normal;
    font-stretch: normal;
    letter-spacing: 0.2px;
    text-align: center;
    color: #ffea6a;
  }
  
  .super.logo,
  .hoidjad.logo {
    color: rgba(0, 0, 0, 0.8);
  }
  
  .navbar-nav&gt;li&gt;a {
    font-family: LeagueSpartan;
    letter-spacing: 0.4px;
    color: rgba(0, 0, 0, 0.8);
    padding-top: 20px;
  }
  
  .navbar-nav&gt;.active&gt;a,
  .navbar-nav&gt;.active&gt;a:hover {
    text-decoration: none;
    font-weight: bold;
    border-radius: 0px;
    border-bottom-width: 4px;
    border-bottom-style: solid;
    border-bottom-color: #ffea6a;
    color: #000 !important;
    background-color: transparent !important;
  }
  
  .search-nanny {
    line-height: 7px !important;
    background-color: #ffea6a;
    width: 175px;
    height: 42px;
    margin-top: 7px;
  }
  
  .search-nanny&gt;a:hover,
  .search-nanny&gt;a:focus {
    background-color: #ffea6a;
    height: 40px;
  }
  
  .nav.navbar-item&gt;li&gt;a.nav-link,
  .nav.navbar-item&gt;li&gt;a.nav-link {
    background-color: transparent;
  }
  
  .primary.navbar.navbar-item,
  .primary.navbar&gt;.navbar-item {
    padding-left: 0;
    padding-right: 0;
    border-radius: 1px;
    background-color: #ffea6a;
    width: 175px;
    height: 42px;
  }
  
  .nav.navbar-item&gt;li&gt;a.search-nanny,
  .nav.navbar-item&gt;li&gt;a.search-nanny {
    background-color: #ffea6a;
    padding-left: 40px;
  }
  
  
  /* OVERWRITE NAVBAR SO IT WONT COLLAPSE*/
  
  .navbar-collapse.collapse {
    display: block!important;
  }
  
  .navbar-nav&gt;li,
  .navbar-nav {
    float: left !important;
  }
  
  .navbar-right {
    float: right!important;
  }
  
  .navbar {
    margin-bottom: 0px;
    min-height: 100px;
    -webkit-transition: top 0.2s ease-out, padding 0s ease-in-out;
    -moz-transition: top 0.2s ease-out, padding 0s ease-in-out;
    transition: top 0.2s ease-out, padding 0s ease-in-out;
  }
  
  .navbar-fixed-top {
    padding: 0;
    -webkit-box-shadow: 0 1px 3px 0 rgba(154, 154, 154, 0.5) !important;
    -moz-box-shadow: 0 1px 3px 0 rgba(154, 154, 154, 0.5) !important;
    box-shadow: 0 1px 3px 0 rgba(154, 154, 154, 0.5) !important;
    background-image: none;
    background-color: #fff;
    background-repeat: repeat-x;
    box-shadow: none;
    min-height: 56px;
  }
  
  .index-header-right-row {
    width: 75%;
    margin: 0 auto;
  }
  
  
  /************************
  FOOTER
  ************************/
  
  .social-media-icons {
    float: right;
    position: absolute;
    bottom: 60px;
    right: 94px;
  }
  
  .footer-text {
    font-family: LeagueSpartan;
    text-align: center;
    padding: 57px 0 28px 0;
  }
  
  .footer-text-paragraph {
    font-size: 14px;
    font-weight: normal;
    font-style: normal;
    font-stretch: normal;
    text-align: center;
    color: #dbc5da;
  }
  
  .company-name {
    color: #ffea6a;
  }
  
  .footer-area {
    background: #af66c8;
    padding-bottom: 20px;
    color: white;
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    overflow: hidden;
    height: 164px;
    z-index: 999;
  }
  
  
  /*******************************
  responsive section starts
  *******************************/
  
  @media only screen and (min-width: 321px) {}
  
  @media only screen and (max-width: 991px) {
    .index-header-right-row {
      width: 100%;
      margin: 0 auto;
    }
    .navbar-nav&gt;li&gt;a {
      padding-top: 10px;
    }
    .nav.navbar-item&gt;li&gt;a.search-nanny,
    .nav.navbar-item&gt;li&gt;a.search-nanny {
      background-color: transparent;
      padding-left: 0px;
    }
    .content-area {
      margin-top: 6.5rem;
    }
    .social-media-icons {
      float: right;
      position: absolute;
      bottom: 150px;
      margin-left: auto;
      margin-right: auto;
      left: 0;
      right: 0;
    }
    .footer-text {
      padding: 115px 0 35px 0;
    }
    .footer-area {
      height: 230px;
    }
    .collapse.navbar-collapse {
      height: 56px;
    }
    .search-nanny {
      width: 120px;
      background-color: transparent;
      color: #9113bb;
    }
    .navbar-brand {
      display: none;
    }
    .navbar-right {
      display: inline-block;
      float: none !important;
      vertical-align: top;
    }
    .collapse.navbar-collapse {
      text-align: center;
    }
    #header-top-logo {
      display: block;
    }
    .navbar-btn {
      width: 115px;
    }
  }
  
  @media screen and (max-width: 768px) {}
  
  @media only screen and (min-device-width: 768px) and (max-device-width: 1024px) and (orientation: landscape) {}
  
  @media only screen and (min-device-width: 768px) and (max-device-width: 1024px) and (orientation: portrait) {}
  
  @media only screen and (min-width: 1224px) {}
  
  @media only screen and (min-width: 1824px) {}</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"&gt;&lt;/script&gt;
  &lt;link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/&gt;
  &lt;div class="row" id="header-top-logo"&gt;
    &lt;p&gt;&lt;a href="/"&gt;&lt;span class="super"&gt;super&lt;/span&gt; &lt;span class="hoidjad"&gt;hoidjad&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;
  &lt;nav class="navbar navbar-fixed-top primary navbar"&gt;
    &lt;div class="container"&gt;
      &lt;div class="col-md-12"&gt;
        &lt;!-- Brand and toggle get grouped for better mobile display --&gt;
        &lt;!-- Collect the nav links, forms, and other content for toggling --&gt;
        &lt;div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"&gt;
  
          &lt;ul class="nav navbar-nav navbar-right navbar-item"&gt;
            &lt;li class="nav-item"&gt;&lt;a class="nav-link" href=""&gt;Meist&lt;/a&gt;&lt;/li&gt;
            &lt;li class="nav-item"&gt;&lt;a class="nav-link" href=""&gt;Artiklid&lt;/a&gt;&lt;/li&gt;
            &lt;li class="nav-btn"&gt;&lt;a href="#" data-toggle="modal" data-target="#searchNannyModel" class="search-nanny"&gt;Otsi hoidjat&lt;/a&gt;&lt;/li&gt;
  
          &lt;/ul&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/nav&gt;</code></pre>
  </div>
  </div>
  </p>