Sticky Navigation Bar with jQuery and Bootstrap
File Size: | 3.35 KB |
---|---|
Views Total: | 78302 |
Last Update: | |
Publish Date: | |
Official Website: | Go to website |
License: | MIT |
With a little CSS and jQuery magic, we can make a native Bootstrap navbar sticky at the top of the document as you scroll down the page.
How to use it:
1. Include the jQuery library and Twitter's Bootstrap 3 in the document.
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="//netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
2. Create a standard Boostrap navbar.
<nav class="navbar navbar-default" role="navigation"> <div class="container-fluid"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse-main"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#"></a> </div> <div class="collapse navbar-collapse" id="navbar-collapse-main"> <ul class="nav navbar-nav navbar-right"> <li><a href="javascript:void(0);">Home</a></li> <li><a href="javascript:void(0);">Menu Item #2</a></li> <li><a href="javascript:void(0);">Menu Item #3</a></li> <li><a href="javascript:void(0);">Menu Item #4</a></li> <li><a href="javascript:void(0);">Menu Item #5</a></li> </ul> </div> </div> </nav>
3. The Javascript to make the navbar sticky after scrolling down the page.
$(document).ready(function(){ $(window).bind('scroll', function() { var navHeight = 300; // custom nav height ($(window).scrollTop() > navHeight) ? $('nav').addClass('goToTop') : $('nav').removeClass('goToTop'); }); });
4. The required CSS to position the navbar.
.goToTop { position: fixed; top: 0; height: 70px; z-index: 1; }
This awesome jQuery plugin is developed by minorsolis. For more Advanced Usages, please check the demo page or visit the official website.