toggle navbar

In this example below you will see how to do a toggle navbar with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by piyush0966, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright piyush0966 ©

Technologies

  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>toggle navbar</title>
  
  
  <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css'>
<link rel='stylesheet prefetch' href='https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css'>

      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <body>
<div class = 'wrapper'>
  <header>
  <nav>
    <div class = 'menu-icon'>
      <i class = 'fa fa-bars fa-2x'></i>
    </div>
     <div class = 'logo'>Logo</div>
    <div class = 'menu'>
    <ul>
      <li ><a href = '#'  class = 'hoverMenu'>Product<span class='fa fa-caret-down'></span></a>
       <ul class='sub-menu'>
         <li><a href = '#'>Feedback</a></li>
         <li><a href = '#'>Referral</a></li>
        <li><a href = '#'>Checkin</a></li>
      </ul> 
        </li>
      <li><a href = '#' class = 'test'>Career</a>
      </li>
      <li><a href = '#'>Dashboard</a></li>
      <li><a href = '#'>Contact Us</a></li>
    </ul>
    </div>
  </nav>
  </header>
  <div class = 'content'>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    <p>
      It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
    </p>
  </div>
</div>
</body>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/piyush0966/toggle-navbar-zRZVBQ */
/:root {
  font-size:10px;
}
html, body {
  paddinig:0px;
  margin:0px;
}
.wrapper {
  width:100%;
  height:auto;
}
header {
  background: url(http://hdqwalls.com/wallpapers/nature-background.jpg) no-repeat 50% 50%;
  background-size: cover;
  height:100vh;
  width:100%;
}

.content {
  font-size:1.2rem;
  line-height:2.5rem;
  padding:1rem;
  text-align:justify;
}
.logo {
  position: fixed;
  margin: 16px 36px;
  font-weight: bold;
  font-size: 2.4rem;
  color:#fff;
}

nav {
  position: fixed;
  width:100%;
}

ul {
  margin:0px !important;
  padding:0px !important;
}

nav ul {
  float:right;
  list-style:none;
  backgroung: rgba(0,0,0,0);
  padding:0px;
  margin:0px;
  color:#fff;
  text-align:center;
  overflow:hidden;
   transition:1s;
  
}

nav ul ul {
  float:none;
}
nav.black  {
  background:#000;
  opacity:0.8;
}
nav ul li {
  float:left;
/*   display:inline-block; */
   padding-top:20px; 
  width:150px;
/*   background-color:green; */
}

 .sub-menu {
  background-color:white;
  border-radius:5px;
  text-lign:left;
  max-width:150px;
/*   padding-top:10px !important; */
}



.sub-menu li {
  margin-left:10px;
}

.sub-menu a {
  color:black !important;
  font-size:1.4rem;
}

.hoverMenu span {
  margin-left:5px;
}
nav ul li a {
  text-decoration:none;
  color:#fff;
/*   background-color:red; */
  font-size:2rem;
}
 nav ul li a:hover, nav ul li a:link, nav ul li a:visited {
  text-decoration:none;
  color:#fff;
}

nav ul li ul {
   display:grid;
}
nav ul li ul li{
  float:left;
  height:50px;
  width:auto;
 display:none;
  padding: 0px !important;
}
/*  nav ul li:hover .sub-menu li{
  display:block;
} */

.show {
  display:block;
}
.menu-icon {
  width:100%;
  background: #000;
  text-align:right;
  box-sizing: border-box;
  cursor:pointer;
  padding:15px 24px;
  color:#fff;
  display:none;
}

@media(max-width:580px) {
  .logo {
    position:fixed;
    top:0;
    margin-top:16px;
  }
  nav ul {
    max-height:0px;
    background:#000;
    width:100%;
     transition:1s;
  }
  nav.black ul {
    background:#000;
  }
  .showing {
    max-height:60rem;
/*     transition:1s; */
/*      float:none; */
     
  }
  nav ul li {
    box-sizing:border-box;
    width:100%;
    padding: 24px 10px;
    text-align:left;
  }
  .menu-icon {
    display:block;
/*     background-color:#eee; */
  }
}

/*Downloaded from https://www.codeseek.co/piyush0966/toggle-navbar-zRZVBQ */
/*toogle*/
$(document).ready(function() {
  $('.menu-icon').on('click', function(){
   $('nav ul').toggleClass('showing'); 
    // $('nav ul ul').removeClass('sub-menu');
  });
});

/*scroll*/
$(window).on('scroll', function(){
  if($(window).scrollTop()) {
     $('nav').addClass('black');
  }
  else {
    $('nav').removeClass('black');
  }
});


/*toggle */
$('li a span').on('click',function(){
   if ($('nav ul ul li').hasClass('show')) {
     $('nav ul ul li').removeClass('show');
   } else {
   $('nav ul ul li').addClass('show');
   }
});

Comments