In this post we will create a fixed navbar that disappears when the user scrolls down and reappears upon scrolling up using Bootstrap and jQuery.
Try it Yourself / Demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
/* simplecodetips.com - Static Navbar */ .navbar-brand { font-size: 21px; font-weight: bolder; } .slidd { position: fixed; width: 100%; padding: 0px; margin: 0px; border: 0px; } .navbar-custom { background-color: #0c4d8a !important; color: #ffffff; border-radius: 0; } .navbar-custom .navbar-nav > li > a { color:#fff; background-color: #0c4d8a; } .navbar-nav { background-color: #0c4d8a; font-size: 12.6px; margin-left: 21px; } .navbar-custom .navbar-nav > .active > a, .navbar-nav > .active > a:hover, .navbar-nav > .active > a:focus { color: #ffffff; } .navbar-custom .navbar-nav > li > a:hover, .nav > li > a:focus { text-decoration: none; background-color: #1c5c99; } .navbar-custom .navbar-brand { color:#fff; } .navbar-brand a { color: #fff; } .navbar-custom .navbar-toggle { background-color:#eeeeee; } .navbar-custom .icon-bar { background-color:#0c4d8a; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<header> <nav class="navbar navbar-custom navbar-fixed-top slidd"> <div class="container-fluid"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button><a class="navbar-brand" href="https://simplecodetips.com/" rel="home">Logo</a> </div> <div class="collapse navbar-collapse" id="myNavbar"> <ul class="nav navbar-nav"> <li><a href="#">About Us</a></li> <li><a href="#">Contact</a></li> <li><a href="#">Services</a></li> <li><a href="#">Gallery</a></li> <li><a href="#">Forum</a></li> </ul> </div> </div> </nav> </header> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
if ( $(window).width() > 769) { var mywindow = $(window); var mypos = mywindow.scrollTop(); var up = false; var newscroll; mywindow.scroll(function () { newscroll = mywindow.scrollTop(); if (newscroll > mypos && !up) { $('.slidd').stop().fadeOut(730); up = !up; console.log(up); } else if(newscroll < mypos && up) { $('.slidd').stop().fadeIn(300); up = !up; } mypos = newscroll; }); } |
You can alter the fadeIn()
and fadeOut()
or change them to other jQuery effect.
Change the background-color and the hover color and so on from the CSS file to fit your project needs and aesthetics.