Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap 3 using affix to fixing navbar position to top when scrolling

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.

like image 847
kjaer108 Avatar asked Oct 31 '13 21:10

kjaer108


People also ask

How do you make a sticky navigation bar that becomes fixed to the top after scrolling?

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.

How do I keep my bootstrap navbar always on top?

In the navbar div , you should add the class name . navbar-fixed-top .

How do I keep the navbar always on 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.


1 Answers

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

like image 109
Mo. Avatar answered Oct 19 '22 04:10

Mo.