Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was having a similar problem, and I believe I found an improved solution.</p> <p>Don't bother specifying <code>data-offset-top</code> in your HTML. Instead, specify it when you call <code>.affix()</code>:</p> <pre><code>$('#nav').affix({ offset: { top: $('#nav').offset().top } });​ </code></pre> <p>The advantage here is that you can change the layout of your site without needing to update the <code>data-offset-top</code> 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.</p> <hr> <p>You will still need to clamp the element to the top with CSS. Furthermore, I had to set <code>width: 100%</code> on the nav element since <code>.nav</code> elements with <code>position: fixed</code> misbehave for some reason:</p> <pre><code>#nav.affix { position: fixed; top: 0px; width: 100%; } </code></pre> <hr> <p>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 <code>div</code> whose height I set to be equal to the navbar at runtime:</p> <pre><code>&lt;div id="nav-wrapper"&gt; &lt;div id="nav" class="navbar"&gt; &lt;!-- ... --&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>.</p> <pre><code>$('#nav-wrapper').height($("#nav").height()); </code></pre> <hr> <p><a href="http://jsfiddle.net/namuol/Uaa3U/">Here's the obligatory jsFiddle to see it in action</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload