JKR
JKR

Reputation: 99

Hide Elements and Disable Scroll when Mobile Navbar is open

So, I'm having trouble with my mobile navbar. It is a simple, responsive navbar. But when I open it on mobile, the elements on the body are shown. This gif shows everything 👇 enter image description here

I'm using this code for the navbar:

HTML

 <div class="container">
<div class="logo">
  <a href="#"><img src="logo.png" alt="logo"></a>
</div>
<div class="navbar">

  <div class="icon-bar" onclick="Show()">
    <i></i>
    <i></i>
    <i></i>
  </div>

  <ul id="nav-lists">
    <li class="close"><span onclick="Hide()">X</span></li>
    <li><a href="#">Home</a></li>
    <li><a href="#">Blog</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Contact</a></li>
  </ul>

</div>

CSS:

      *::before,
  *::after {
    box-sizing: border-box;
    -webkit-box-sizing: border-box;
  }

  .container {
    height: 60px;
    background-color: #333333;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    overflow: hidden;    
  }
  
  .container .logo {
    max-width: 250px;
    padding: 0 10px;
    overflow: hidden;
  }
  
  .container .logo a {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    height: 60px;
  }
  
  .container .logo a img {
    max-width: 100%;
    max-height: 60px;
  }
  
  .container .navbar {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
    -webkit-box-flex: 1;
    -ms-flex: 1;
    flex: 1;
    padding-bottom: 5px;
  } 
  .container .navbar ul {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
    list-style: none;
    margin: 0;
    padding: 0;
  }  
  .container .navbar ul li a {
    text-decoration: none;
    color: #999999;
    font-size: 14px;
    text-transform: uppercase;
    display: block;
    height: 60px;
    line-height: 60px;
    cursor: pointer;
    padding: 0 10px;
  } 
  .container .navbar ul li a:hover {
    color:#4B088A;
    background-color: rgba(23, 23, 23, 0.9);
  }
  .container .navbar ul .close {
    display: none;
    text-align: right;
    padding: 10px;
  }
  .container .navbar ul .close span {
    font-size: 40px;
    display: inline-block;
    border: 1px solid #cccccc;
    padding: 0 10px;
    cursor: pointer;
  }  
  .container .navbar .icon-bar {
    padding: 18px 8px;
    width: 50px;
    height: 60px;
    display: none;
    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
    -ms-flex-direction: column;
    flex-direction: column;
    -webkit-box-pack: justify;
    -ms-flex-pack: justify;
    justify-content: space-between;
    cursor: pointer;
  }  
  .container .navbar .icon-bar i {
    background-color: #ffffff;
    height: 2px;
  }
  
  @media only screen and (max-width: 650px) {
   .container {
      -webkit-box-pack: justify;
      -ms-flex-pack: justify;
      justify-content: space-between;
      touch-action: none;
      overflow: hidden;
    }
    .container .logo {
      -webkit-box-flex: 1;
      -ms-flex: 1;
      flex: 1;
    }
    .container .navbar {
      -webkit-box-flex: 0;
      -ms-flex: 0;
      flex: 0;     
    }
    .container .navbar ul {
      -ms-flex-wrap: nowrap;
      flex-wrap: nowrap;
      position: fixed;
      left: 100%;
      -webkit-box-orient: vertical;
      -webkit-box-direction: normal;
      -ms-flex-direction: column;
      flex-direction: column;
      background: rgb(32, 21, 30);
      width: 100%;
      height: 100%;
      overflow: auto;
      -webkit-transition: left .3s;
      -o-transition: left .3s;
      transition: left .3s;
    }
    .container .navbar ul li a {
      padding: 10px;
      font-size: 16px;
      height: auto;
      line-height: normal;
      color: #555555;
    }
    .container .navbar ul .close {
      display: block;
    }
    .container .navbar .icon-bar {
      display: -webkit-box;
      display: -ms-flexbox;
      display: flex;
    }
    .container .navbar ._Menus-show {
      left: 0;
    }
    @media screen and (max-width:875px) {
      .navbar.responsive {
        position: fixed;
        width: 100%;
        height: 100px;
        background-color: rgba(236, 201, 205, 1);
        transition: background-color .6s;
   
      }
    }
}

And JavaScript!

     var navList = document.getElementById("nav-lists");
  const modal = document.querySelector("#modal");

var body = body;

function Show() {
  navList.classList.add("_Menus-show");
  body.style.overflow = "hidden";

}

function Hide() {
  navList.classList.remove("_Menus-show");
  body.style.overflow = "auto";
  

}

Summary: I want to hide elements on the body when navbar is open and disable scrolling. Please take a look at the Gif for a better understanding.

Sorry for bad English, and Thanks in advance!

Upvotes: 0

Views: 984

Answers (3)

Eloi
Eloi

Reputation: 46

You have a probleme with your var body = body. You can't fetch the html body like this. You should fetch the body element like this : let body = document.querySelector('body').

Your Show function is ok.

Upvotes: 1

user17405784
user17405784

Reputation:

This code whats you linked is to small(I mean try this code it's work like you want). From gif I think the problem is in css. Try add position: absoulte/fixed; z-index: 1 to navbar. In js try this solution:

let isNavbarActive = false;
document.querySelector('ul.nav-lists').addEventListener('click', ()=> { 
   isNavbarActive = !isNavbarActive
   if (isNavbarActive) {
      document.body.style.overflowY = 'hidden';
      navList.classList.add("_Menus-show");
   } else {
      navList.classList.remove("_Menus-show");
      document.body.style.overflowY = '';
   }
})

Upvotes: 1

Mr Proud
Mr Proud

Reputation: 50

Add an id to your body tag name "body"

Try doing this

<body id="body">
..Your code goes here
</body>

<script>
var body = document.getElementById("body");
 // your javascript
</script>

Upvotes: 1

Related Questions