skip to Main Content

I’m using a Bootstrap navbar which hides when the user scrolls down and shows when the user scrolls up. Now I want to add a shadow if the navbar shows again after some scrolling.

There are two CSS classes for the showing/hiding of the navbar: .scrolled-down and .scrolled-up.

The problem with my code is that the .scrolled-up class stays even if the user is back at the very top of the page. If I add a shadow to that class, it stays even if the user is back at the top.

Is there an option to add a third class like .on-top? I could use box-shadow: none !important for that and give the other two a shadow.

Here’s a working example of my code:

And here’s the code: https://codepen.io/cray_code/pen/qBMQdeo

CSS:

.scrolled-down {
    transform: translateY(-100%);
    transition: all 0.3s ease-in-out;
    background: red !important;
}
.scrolled-up {
    transform: translateY(0);
    transition: all 0.3s ease-in-out;
    background: green !important;
}
.autohide {
    position: fixed !important;
    top: 0;
    right: 0;
    left: 0;
    width: 100%;
    z-index: 1030;
}

Javascript:

document.addEventListener("DOMContentLoaded", function(){

  el_autohide = document.querySelector('.autohide');

  // add padding-top to bady (if necessary)
  navbar_height = document.querySelector('.navbar').offsetHeight;
  document.body.style.paddingTop = navbar_height + 'px';

  if(el_autohide){
    var last_scroll_top = 0;
    window.addEventListener('scroll', function() {
          let scroll_top = window.scrollY;
         if(scroll_top < last_scroll_top) {
              el_autohide.classList.remove('scrolled-down');
              el_autohide.classList.add('scrolled-up');
          }
          else {
              el_autohide.classList.remove('scrolled-up');
              el_autohide.classList.add('scrolled-down');
          }
          last_scroll_top = scroll_top;
    });
    // window.addEventListener
  }
  // if

});

The code is from here: https://bootstrap-menu.com/detail-autohide.html

2

Answers


  1. Add another if statement.

    if (window.scrollY == 0) {
       el_autohide.classList.remove('scrolled-up');
       el_autohide.classList.remove('scrolled-down');
    }
    

    All together. Demo

    document.addEventListener("DOMContentLoaded", function() {
    
      el_autohide = document.querySelector('.autohide');
    
      // add padding-top to bady (if necessary)
      navbar_height = document.querySelector('.navbar').offsetHeight;
      document.body.style.paddingTop = navbar_height + 'px';
    
      if (el_autohide) {
        var last_scroll_top = 0;
        window.addEventListener('scroll', function() {
          let scroll_top = window.scrollY;
          if (scroll_top < last_scroll_top) {
            el_autohide.classList.remove('scrolled-down');
            el_autohide.classList.add('scrolled-up');
          } else {
            el_autohide.classList.remove('scrolled-up');
            el_autohide.classList.add('scrolled-down');
          }
          if (window.scrollY == 0) {
            el_autohide.classList.remove('scrolled-up');
            el_autohide.classList.remove('scrolled-down');
          }
          last_scroll_top = scroll_top;
        });
        // window.addEventListener
      }
      // if
    
    });
    /* bg-blue for SO demo only */
    
    .bg-blue {
      background-color: #0d6efd!important;
    }
    
    .scrolled-down {
      transform: translateY(-100%);
      transition: all 0.3s ease-in-out;
      background-color: red !important;
    }
    
    .scrolled-up {
      transform: translateY(0);
      transition: all 0.3s ease-in-out;
      background-color: green !important;
    }
    
    .autohide {
      position: fixed !important;
      top: 0;
      right: 0;
      left: 0;
      width: 100%;
      z-index: 1030;
    }
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" />
    <div class="container">
    
      <!-- ============= COMPONENT ============== -->
      <nav class="autohide navbar navbar-expand-lg navbar-dark bg-blue">
        <div class="container-fluid">
          <a class="navbar-brand" href="#">Brand</a>
          <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#main_nav" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
          <div class="collapse navbar-collapse" id="main_nav">
            <ul class="navbar-nav">
              <li class="nav-item active"> <a class="nav-link" href="#">Home </a> </li>
              <li class="nav-item"><a class="nav-link" href="#"> About </a></li>
              <li class="nav-item"><a class="nav-link" href="#"> Services </a></li>
              <li class="nav-item dropdown">
                <a class="nav-link  dropdown-toggle" href="#" data-bs-toggle="dropdown">  More items  </a>
                <ul class="dropdown-menu">
                  <li><a class="dropdown-item" href="#"> Submenu item 1</a></li>
                  <li><a class="dropdown-item" href="#"> Submenu item 2 </a></li>
                  <li><a class="dropdown-item" href="#"> Submenu item 3 </a></li>
                </ul>
              </li>
            </ul>
            <ul class="navbar-nav ms-auto">
              <li class="nav-item"><a class="nav-link" href="#"> Menu item </a></li>
              <li class="nav-item"><a class="nav-link" href="#"> Menu item </a></li>
              <li class="nav-item dropdown">
                <a class="nav-link  dropdown-toggle" href="#" data-bs-toggle="dropdown"> Dropdown right </a>
                <ul class="dropdown-menu dropdown-menu-end">
                  <li><a class="dropdown-item" href="#"> Submenu item 1</a></li>
                  <li><a class="dropdown-item" href="#"> Submenu item 2 </a></li>
                </ul>
              </li>
            </ul>
          </div>
          <!-- navbar-collapse.// -->
        </div>
        <!-- container-fluid.// -->
      </nav>
      <!-- ============= COMPONENT END// ============== -->
    
      <section class="section-content py-5">
    
        <h6>Demo for Screen dim on navbar menu dropdown active. <br> Based on Bootstrap 5 CSS framework. </h6>
        <p>For this demo page you should connect to the internet to receive files from CDN like Bootstrap5 CSS, Bootstrap5 JS</p>
    
        <p class="text-muted"> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
          dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing
          elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
          dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p>
    
        <p class="text-muted"> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
          dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing
          elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
          dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p>
    
        <p class="text-muted"> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
          dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing
          elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
          dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p>
    
        <p class="text-muted"> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
          dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing
          elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
          dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p>
    
        <p class="text-muted"> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
          dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing
          elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
          dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p>
    
        <p class="text-muted"> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
          dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing
          elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
          dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p>
    
        <p class="text-muted"> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
          dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing
          elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
          dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p>
    
        <p class="text-muted"> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
          dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing
          elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
          dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p>
    
        <a href="https://bootstrap-menu.com/detail-darken-onshow.html" class="btn btn-success"> &laquo Back to tutorial or Download code</a>
    
      </section>
    
    </div>
    <!-- container //  -->
    Login or Signup to reply.
  2. I hope that was your intention:

    document.addEventListener("DOMContentLoaded", function(){
    
              el_autohide = document.querySelector('.autohide');
    
              // add padding-top to bady (if necessary)
              navbar_height = document.querySelector('.navbar').offsetHeight;
              document.body.style.paddingTop = navbar_height + 'px';
    
              if(el_autohide){
                var last_scroll_top = 0;
                window.addEventListener('scroll', function() {
                      let scroll_top = window.scrollY;
                        if(scroll_top == 0){
                            el_autohide.classList.remove('scrolled-down');
                            el_autohide.classList.add('scrolled-up');
                            el_autohide.classList.add('scrolled-top');
                        } else if(scroll_top < last_scroll_top) {
                          el_autohide.classList.remove('scrolled-down');
                          el_autohide.classList.add('scrolled-up');
                      } else{
                          el_autohide.classList.remove('scrolled-up');
                            el_autohide.classList.remove('scrolled-top');
                          el_autohide.classList.add('scrolled-down');
                      }
                      last_scroll_top = scroll_top;
                });
              }
            });
    .scrolled-down {
        transform: translateY(-100%);
        transition: all 0.3s ease-in-out;
        background: red !important;
    }
    .scrolled-up {
        transform: translateY(0);
        transition: all 0.3s ease-in-out;
        background: green !important;
        box-shadow: 10px 10px 10px black;
    }
    
    .scrolled-top {
        box-shadow: none;
    }
    .autohide {
        position: fixed !important;
        top: 0;
        right: 0;
        left: 0;
        width: 100%;
        z-index: 1030;
    }
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" />
    <div class="container">
    
    <!-- ============= COMPONENT ============== -->
    <nav class="autohide navbar navbar-expand-lg navbar-dark bg-primary">
     <div class="container-fluid">
         <a class="navbar-brand" href="#">Brand</a>
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#main_nav"  aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
      <div class="collapse navbar-collapse" id="main_nav">
        <ul class="navbar-nav">
            <li class="nav-item active"> <a class="nav-link" href="#">Home </a> </li>
            <li class="nav-item"><a class="nav-link" href="#"> About </a></li>
            <li class="nav-item"><a class="nav-link" href="#"> Services </a></li>
            <li class="nav-item dropdown">
                <a class="nav-link  dropdown-toggle" href="#" data-bs-toggle="dropdown">  More items  </a>
                <ul class="dropdown-menu">
                  <li><a class="dropdown-item" href="#"> Submenu item 1</a></li>
                  <li><a class="dropdown-item" href="#"> Submenu item 2 </a></li>
                  <li><a class="dropdown-item" href="#"> Submenu item 3 </a></li>
                </ul>
            </li>
        </ul>
        <ul class="navbar-nav ms-auto">
            <li class="nav-item"><a class="nav-link" href="#"> Menu item </a></li>
            <li class="nav-item"><a class="nav-link" href="#"> Menu item </a></li>
            <li class="nav-item dropdown">
                <a class="nav-link  dropdown-toggle" href="#" data-bs-toggle="dropdown"> Dropdown right </a>
                <ul class="dropdown-menu dropdown-menu-end">
                  <li><a class="dropdown-item" href="#"> Submenu item 1</a></li>
                  <li><a class="dropdown-item" href="#"> Submenu item 2 </a></li>
                </ul>
            </li>
        </ul>
      </div> <!-- navbar-collapse.// -->
     </div> <!-- container-fluid.// -->
    </nav>
    <!-- ============= COMPONENT END// ============== -->
    
    <section class="section-content py-5">
    
            <h6>Demo for Screen dim on navbar menu dropdown active. <br> Based on Bootstrap 5 CSS framework.  </h6>
            <p>For this demo page you should connect to the internet to receive files from CDN  like Bootstrap5 CSS, Bootstrap5 JS</p>
           
            <p class="text-muted"> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
            tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
            quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
            consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
            cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
            proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
            tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
            quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
            consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
            cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
            proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p>
    
    <a href="https://bootstrap-menu.com/detail-darken-onshow.html" class="btn btn-success"> &laquo Back to tutorial or Download code</a>
    
    </section>
    
    </div><!-- container //  -->
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search