title: Why is the height of content loaded through AJAX infinity scroll not measured correctly after loading?
body: <p>I am loading posts composed of pictures or videos into the main div of my website with <em>infinity scroll</em> (implemented through AJAX). The system works pretty much like on any popular meme website.</p>
  
  <p><strong>The Problem:</strong> Each post has a body section on the left and a social button container on the right. I want the social button container to move alongside the body of the post as the user scrolls, and stop at the bottom of the parent div so that they can like it when they reach the bottom of the post. The problem is that <em>some</em> of the posts loaded through my AJAX scroll function don't seem to calculate the parents height properly and they either never move or jump straight to the bottom of the parent div when the top of the browser window reaches them. <a href="http://i.imgur.com/KNCbUmf.png">Here is a visual illustration</a> of the desired behavior.</p>
  
  <p><strong>Implementation:</strong> I wasn't able to compile a working jsFiddle that would simulate the behavior of the AJAX requests loading images and videos combined with the on "scroll" calls. Here is my implementation and debugging attempts:</p>
  
  <p>Each article has the following structure:</p>
  
  <pre><code>&lt;div class="article-container"&gt;
     &lt;div class="article-body-left"&gt;&lt;img src="..."&gt;&lt;/div&gt;
     &lt;div class="social-buttons-right"&gt;
        &lt;div class="facebook-button"&gt;...&lt;/div&gt;
     &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>After each AJAX request is complete I try to use on 'scroll' to determine each of the social button containers positions and modify them as the user scrolls using ajaxComplete():</p>
  
  <pre><code>$(document).ajaxComplete(function() {
    bind_sticky_boxes();
  }); 
  
  function bind_sticky_boxes() {
    $(window).on('scroll', function() {
      $('.social-buttons-right').not('.following').each( function() { 
          var element = $(this).addClass('following'),
          originalY = element.offset().top,   
          parent_offset = element.parent().offset().top,
          parent_height = element.parent().height(),
          element_height = element.height(),
          destination = originalY+parent_height-element.height() -10;
  
            $(window).on('scroll', function(event) {
                var scrollTop = $(window).scrollTop();
                if (scrollTop &gt; originalY - 10){
                   element.css('position','absolute');
                   element.css('top',(scrollTop - parent_offset + 10) + 'px');
                   element.css('right', '0px');
                   if (scrollTop &gt; (destination -10 ) ) {
                        element.css('top',parent_height-element_height);
                        element.css('right', '0px'); 
                        element.css('bottom', '0px');
                   }
                } else {
                   element.css('position','relative');
                   element.css('top','initial');
                   element.css('left', 'initial');
                   element.css('right','initial');
                   element.css('bottom', 'initial');
                } 
            });
      });
      });
  }
  </code></pre>
  
  <p><strong>Debugging and notes:</strong> The scroll function works fine after the first AJAX request, but somewhat breaks afterwards as some of the <em>.social-buttons-right</em> don't follow along the article body or jump straight to the bottom of the parent div during site scroll.</p>
  
  <p>When I inspected the elements that are misbehaving I found, that they all have the ".following", meaning that the scroll function has found them, but they also get a bad mix of the css properties that bind_sticky_boxes gives them.</p>
  
  <p>This lead me to believe that it has something to do with height calculation during the image load, so I used requestanimationframe() to wait a little and then setTimeout(function(){..},2000) to trigger the scroll bind even later. None of these really resolved the issue (the latter one wasn't a solution anyway.) I have also tried to trigger the AJAX requests a bit sooner, but that seemed to have an even worse effect for some reason.</p>
  
  <p><strong>Update:</strong> I am now convinced that the issue is related to the parent height calculation. I swapped the css properties for unique classes with the same attributes and noticed that the class change for misbehaving elements occurs almost always at the start of the parent div, which I believe to be a strong indication that it's height is not being calculated properly right after appending it to the main div.</p>
  
  <p>In regards to my infinity scroll. I am not including the whole AJAX as I don't think it is strictly related to the issue. But I append the content to the main div this way:</p>
  
  <pre><code>success: function(response){
              if ( max_page &gt;= paged){
                  $('#page').append(response.content);
              } else{
                  has_content = false;
              }
              loading = false;
            }
  </code></pre>
  
  <p>After the page is done loading I initiate my infinity scroll. AJAX requests trigger when user reaches the element with id <em>load-more-posts</em>, which is below the main wrapper.</p>
  
  <pre><code>$(window).on('load',function(){
   $(window).on('scroll', function() {
        var element_position = $('#load-more-posts').offset().top + $('#load-more-posts').outerHeight() - window.innerHeight;
        var y_scroll_pos = $(window).scrollTop();
        if( ( y_scroll_pos &gt;= element_position ) &amp;&amp; !loading) {
            paged++;
            load_more_posts();
        }
    });    
  </code></pre>
  
  <p>});</p>