Search code examples
jquerycssmenusticky

How to add banner above 'Auto hiding navbar on scroll down'?


I found a great auto hiding navbar but I don't know how to correctly add static banner above it. Can anyone help?

Navbar demo:

https://codepen.io/Mhmdhasan/pen/mAdaQE

Navbar script:

$(document).ready(function () {

  'use strict';

   var c, currentScrollTop = 0,
       navbar = $('nav');

   $(window).scroll(function () {
      var a = $(window).scrollTop();
      var b = navbar.height();

      currentScrollTop = a;

      if (c < currentScrollTop && a > b + b) {
        navbar.addClass("scrollUp");
      } else if (c > currentScrollTop && !(a <= b)) {
        navbar.removeClass("scrollUp");
      }
      c = currentScrollTop;
  });

});

Solution

  • You can add a fixed-height banner by just adding a div element before your nav element. The navbar's css has to be adapted to match the height of the banner's div.

    You can then use

    $('.banner').height()
    

    to calculate the additional scrolling height for the navigation bar to disappear.

    Working example (or on Codepen):

    $(document).ready(function () {
      
      'use strict';
      
       var c, currentScrollTop = 0,
           banner = $('.banner'),
           navbar = $('nav');
    
       $(window).scroll(function () {
          var a = $(window).scrollTop();
         
          if (a > banner.height()) {
            navbar.addClass("fixed");
          } else {
            navbar.removeClass("fixed");
          }
         
          var b = navbar.height() + banner.height();
         
          currentScrollTop = a;
         
          if (c < currentScrollTop && a > b + b) {
            navbar.addClass("scrollUp");
          } else if (c > currentScrollTop && !(a <= b)) {
            navbar.removeClass("scrollUp");
          }
          c = currentScrollTop;
      });
      
    });
    body {
      background: #eee;
      min-height: 3000px;
      padding: 0;
      margin: 0;
      font-family: "Open Sans", sans-serif;
    }
    
    .banner {
      width: 100%;
      line-height: 100px;
      background: red;
      color: white;
      text-align: center;
      z-index: 9999;
    }
    
    .container {
      width: 80%;
      margin: 0 auto;
      clear: both;
    }
    
    a {
      display: inline-block;
      color: #333;
      text-decoration: none;
    }
    
    nav {
      background: #fff;
      height: 80px;
      line-height: 80px;
      box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.2);
      width: 100%;
      z-index: 9998;
      transition: all 0.5s;
    }
    nav.fixed {
      top: 0;
      left: 0;
      position: fixed;
    }
    nav.scrollUp {
      transform: translateY(-180px);
    }
    nav ul.navbar-menu {
      margin: 0;
      padding: 0;
      display: inline-block;
      float: right;
    }
    nav ul.navbar-menu li {
      display: inline-block;
      margin: 0 10px;
    }
    nav ul.navbar-menu li a {
      color: #666;
      font-size: 14px;
    }
    nav a#brand {
      text-transform: uppercase;
      foat: left;
      font-weight: 800;
      font-size: 20px;
    }
    nav button {
      background: none;
      width: 30px;
      height: 40px;
      margin-top: 20px;
      border: none;
      float: right;
      display: inline-block;
      cursor: pointer;
      display: none;
    }
    nav button span {
      width: 30px;
      height: 40px;
      height: 2px;
      background: #333;
      display: block;
      margin: 5px 0;
    }
    
    @media (max-width: 768px) {
      nav ul.navbar-menu {
        display: none;
      }
    
      nav button {
        display: block;
      }
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="banner">
      Banner content
    </div>
    <nav>
      <div class="container">
        <a href="#" id="brand">Brand</a>
        <button>
          <span></span>
          <span></span>
          <span></span>
        </button>
        
        <ul class="navbar-menu">
          <li><a href="#">Home</a></li>
          <li><a href="#">page a</a></li>
          <li><a href="#">page b</a></li>
          <li><a href="#">page c</a></li>
          <li><a href="#">page d</a></li>
        </ul>
        
      </div>
    </nav>