title: jQuery offset().top not working properly
body: <p>I am trying to get a div to stick once it is scrolled out of view. </p>
  
  <pre><code>var jQ = jQuery.noConflict();
  
  jQ(document).ready(function() {
  
    var win = jQ(window);
    var navTop = jQ('#navbar').offset().top;
  
    win.scroll(function() {
      jQ('#navbar').toggleClass('sticky', win.scrollTop() &gt; navTop);
    });
  
  });
  </code></pre>
  
  <p>The problem is that with this code, navTop is not calculated correctly. If I calculate navTop in the scroll function it works as expected but with a horrible flickering effect which I assume is due to recalculating the value many times.</p>
  
  <p>Why does it not calculate the value correctly after document is loaded?</p>