This is driving me nuts... This might be simple by I'm not hitting the nail: please give a hint :-)
I'm trying to play with Bootstrap 3 for a new layout for my site. I want the navbar to pin to the top of the screen when I scroll.
I got it working but the content below navbar shifts when the affix class kicks in. I can't find my way around it.
To create an affix or sticky navbar, you need to use HTML, CSS, and JavaScript. HTML will make the structure of the body, CSS will make it looks good. This kind of sticky navbar looks attractive on website. By using JavaScript, you can easily make the navigation bar sticky when the user scrolls down.
In the navbar div , you should add the class name . navbar-fixed-top .
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.
There are two option either javascript or HTML5 data-
attribute
Via data attributes To easily add affix behavior to any element, just add data-spy="affix" to the element you want to spy on. Use offsets to define when to toggle the pinning of an element.
<div data-spy="affix" data-offset-top="60" data-offset-bottom="200">
...
</div>
Via JavaScript Call the affix plugin via JavaScript:
$('#my-affix').affix({
offset: {
top: 100
, bottom: function () {
return (this.bottom = $('.footer').outerHeight(true))
}
}
})
DEMO http://jsfiddle.net/6P5sF/56/
reference http://getbootstrap.com/javascript/#affix-examples
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