title: Get the visible height of a div with jQuery
body: <p>I need to retrieve the visible height of a div within a scrollable area. I consider myself pretty decent with jQuery, but this is completely throwing me off.</p>
  
  <p>Let's say I've got a red div within a black wrapper:</p>
  
  <p><img src="https://i.imgur.com/oiFk3n4.png" /></p>
  
  <p>In the graphic above, the jQuery function would return 248, the visible portion of the div.</p>
  
  <p><img src="https://i.imgur.com/qzHJLZo.png" /></p>
  
  <p>Once the user scrolls past the top of the div, as in the above graphic, it would report 296.</p>
  
  <p><img src="https://i.imgur.com/m8lkMcj.png" /></p>
  
  <p>Now, once the user has scrolled past the div, it would again report 248.</p>
  
  <p>Obviously my numbers aren't going to be as consistent and clear as they are in this demo, or I'd just hard code for those numbers.</p>
  
  <p>I have a bit of a theory:</p>
  
  <ul>
  <li>Get the height of the window</li>
  <li>Get the height of the div</li>
  <li>Get the initial offset of the div from the top of the window</li>
  <li>Get the offset as the user scrolls.
  
  <ul>
  <li>If the offset is positive, it means the top of the div is still visible.</li>
  <li>if it's negative, the top of the div has been eclipsed by the window. At this point, the div could either be taking up the whole height of the window, or the bottom of the div could be showing</li>
  <li>If the bottom of the div is showing, figure out the gap between it and the bottom of the window.</li>
  </ul></li>
  </ul>
  
  <p>It seems pretty simple, but I just can't wrap my head around it. I'll take another crack tomorrow morning; I just figured some of you geniuses might be able to help.</p>
  
  <p>Thanks!</p>
  
  <p>UPDATE: I figured this out on my own, but looks like one of the answers below is more elegant, so I'll be using that instead. For the curious, here's what I came up with:</p>
  
  <pre><code>$(document).ready(function() {
      var windowHeight = $(window).height();
      var overviewHeight = $("#overview").height();
      var overviewStaticTop = $("#overview").offset().top;
      var overviewScrollTop = overviewStaticTop - $(window).scrollTop();
      var overviewStaticBottom = overviewStaticTop + $("#overview").height();
      var overviewScrollBottom = windowHeight - (overviewStaticBottom - $(window).scrollTop());
      var visibleArea;
      if ((overviewHeight + overviewScrollTop) &lt; windowHeight) {
          // alert("bottom is showing!");
          visibleArea = windowHeight - overviewScrollBottom;
          // alert(visibleArea);
      } else {
          if (overviewScrollTop &lt; 0) {
              // alert("is full height");
              visibleArea = windowHeight;
              // alert(visibleArea);
          } else {
              // alert("top is showing");
              visibleArea = windowHeight - overviewScrollTop;
              // alert(visibleArea);
          }
      }
  });
  </code></pre>