The bootstrap documentation on that topic is a little confusing to me. I want to achieve similar behaviour like in the docs with the affix navbar: The navbar is below a paragraph / page heading, and upon scrolling down it should first scroll along until reaching the top of the page, and then stick there fixed for further scrolldowns.
As jsFiddle does not work with the navbar concept, I've set up a separate page for usage as a minimal example: http://i08fs1.ira.uka.de/~s_drr/navbar.html
I use this as my navbar:
<div class="navbar affix-top" data-spy="affix" data-offset-top="50"> <div class="navbar-inner"> <div class="container"> <div class="span12"> <a class="brand" href="#">My Brand</a> This is my navbar. </div> </div> <!-- container --> </div> <!-- navbar-inner --> </div> <!-- navbar -->
I thinkg i would want data-offset-top
to be of value 0 (since the bar should "stick" to the very top" but with 50 there is at least some effect watchable.
If also put the javascript code in place:
<script> $(document).ready (function (){ $(".navbar").affix (); }); </script>
Any help appreciated.
The Affix plugin allows an element to become affixed (locked) to an area on the page. This is often used with navigation menus or social icon buttons, to make them "stick" at a specific area while scrolling up and down the page.
Responsive designWith Bootstrap 2, we've gone fully responsive. Our components are scaled according to a range of resolutions and devices to provide a consistent experience, no matter what.
I was having a similar problem, and I believe I found an improved solution.
Don't bother specifying data-offset-top
in your HTML. Instead, specify it when you call .affix()
:
$('#nav').affix({ offset: { top: $('#nav').offset().top } });
The advantage here is that you can change the layout of your site without needing to update the data-offset-top
attribute. Since this uses the actual computed position of the element, it also prevents inconsistencies with browsers that render the element at a slightly different position.
You will still need to clamp the element to the top with CSS. Furthermore, I had to set width: 100%
on the nav element since .nav
elements with position: fixed
misbehave for some reason:
#nav.affix { position: fixed; top: 0px; width: 100%; }
One last thing: When an affixed element becomes fixed, its element no longer takes up space on the page, resulting in the elements below it to "jump". To prevent this ugliness, I wrap the navbar in a div
whose height I set to be equal to the navbar at runtime:
<div id="nav-wrapper"> <div id="nav" class="navbar"> <!-- ... --> </div> </div>
.
$('#nav-wrapper').height($("#nav").height());
Here's the obligatory jsFiddle to see it in action.
Just implemented this for the first time, and here's what I've found.
The data-offset-top
value is the amount of pixels that you must scroll in order for the affixing effect to take place. In your case, once 50px
is scrolled, the class on your item is changed from .affix-top
to .affix
. You'd probably want to set data-offset-top
to about 130px
in your use case.
Once this class change occurs, you must position your element in css by styling the positioning for class .affix
. Bootstrap 2.1 already defines .affix
as position: fixed;
so all you need to do is add your own position values.
Example:
.affix { position: fixed; top: 20px; left: 0px; }
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