Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fix the bootstrap navbar to the top after a div was scrolled?

I am trying to get my navbar with bootstrap to appear after a certain div, called "A". Div "a" is at the top of by page, and has a height of around 400px. Once the user scrolls past the div "a" and the scrollbar, I want the scroll bar to stick to the top. Similar to something like this: https://www.facebook.com/home but I don't have a video at the top, it is an image.

Can we do that we bootstrap itself?

Thanks!

like image 510
abisson Avatar asked Apr 05 '13 16:04

abisson


People also ask

How do I make the navigation bar stay at the top of Bootstrap?

Set the navbar fixed to the top, add class . navbar-fixed-top to the . navbar class.

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 the navbar at the top of my screen?

Use position absolute and set the top value to the number of pixels you want the Nav bar to be from the top of the browser.


2 Answers

If you'd like to achieve this using Twitter's Bootstrap check out the 'Affix' js. You define the "fixed" point using the "data-offset-top" and then the navbar will become fixed to the top when user scrolls down.

CSS:

#con .well {
    height:390px;
}

#nav.affix {
    position: fixed;
    top: 0;
    width: 100%
}

HTML:

<div class="container" data-spy="affix" data-offset-top="400" id="nav">
    <div id="nav" class="navbar">
        <div class="navbar-inner"> ...

Javascript:

$('#nav').affix();

Here is the working fiddle: http://jsfiddle.net/skelly/df8tb/

Here's a working example (updated for Bootstrap 3): http://bootply.com/90936

like image 66
Zim Avatar answered Sep 27 '22 21:09

Zim


To get the effect show on facebook.com/home your navbar position should be set to static (or relative / absolute) until the user scrolls past 400px and then you need to set it's position to fixed so that is stays at the top of the screen. This can be done with a little javascript.

like image 34
Felix Eve Avatar answered Sep 27 '22 21:09

Felix Eve