I would like to make static navbar to fixed navbar on scroll, when it reaches top of the page.
Is there a way to get it using bootstrap 3 css or javascript?
To create a fixed top menu, use position:fixed and top:0 . Note that the fixed menu will overlay your other content. To fix this, add a margin-top (to the content) that is equal or larger than the height of your menu.
How does Sticky Header work in Bootstrap? Sticky Header is nothing but navigation bar, if we want to make navigation bar becomes stick at the top (sticky header) then use offsetTop within the JavaScript file. Include bootstrap feature in our application we must specify some pre-defined libraries inside our application.
If I'm not wrong, what you're trying to achieve is called Sticky navbar.
With a few lines of jQuery and the scroll event is pretty easy to achieve:
$(document).ready(function() {
var menu = $('.menu');
var content = $('.content');
var origOffsetY = menu.offset().top;
function scroll() {
if ($(window).scrollTop() >= origOffsetY) {
menu.addClass('sticky');
content.addClass('menu-padding');
} else {
menu.removeClass('sticky');
content.removeClass('menu-padding');
}
}
$(document).scroll();
});
I've done a quick working sample for you, hope it helps: http://jsfiddle.net/yeco/4EcFf/
To make it work with Bootstrap you only need to add or remove "navbar-fixed-top" instead of the "sticky" class in the jsfiddle .
Use the affix
component included with Bootstrap. Start with a 'navbar-static-top' and this will change it to fixed
when the height of your header (content above the navbar) is reached...
$('#nav').affix({
offset: {
top: $('header').height()
}
});
http://bootply.com/107973
I am note sure, what you are expecting. Have a look at this fiddle, this might help you.
http://jsfiddle.net/JK52L/8/
You HTML should have the class navbar-fixed-top
or navbar-fixed-bottom
.
HTML
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<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="#">Brand</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
</ul>
</div><!-- /.navbar-collapse -->
</nav>
JS
$(document).scroll(function(e){
var scrollTop = $(document).scrollTop();
if(scrollTop > 0){
console.log(scrollTop);
$('.navbar').removeClass('navbar-static-top').addClass('navbar-fixed-top');
} else {
$('.navbar').removeClass('navbar-fixed-top').addClass('navbar-static-top');
}
});
Here's a solution using Bootstrap's affix plugin:
HTML:
<header class="container-fluid">
...
</header>
<nav class="navbar navbar-default navbar-static-top" role="navigation">
...
</nav>
Javascript:
$('nav').affix({
offset: {
top: $('header').height()
}
});
Set padding-top
to your body
equal to that of your nav
's height so that the content overlayed by the fixed navbar is visible.
$('nav').on('affix.bs.affix', function (){
$('body').css('margin-top', $('nav').height());
});
$('nav').on('affix-top.bs.affix', function (){
$('body').css('margin-top', 0);
});
To get the nav
to stick on top while scrolling add this bit of CSS.
CSS:
.affix
{
top: 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With