Skip to content

Instantly share code, notes, and snippets.

@jmahc
Created August 26, 2016 18:50
Show Gist options
  • Select an option

  • Save jmahc/7cc908d51bc2c48316ad3ff0414e3a98 to your computer and use it in GitHub Desktop.

Select an option

Save jmahc/7cc908d51bc2c48316ad3ff0414e3a98 to your computer and use it in GitHub Desktop.
This gets the height of any element relative to what is visible on screen
function getVisibleHeight(el) {
var elementHeight = el.offsetHeight;
var windowHeight = window.innerHeight;
var rect = el.getBoundingClientRect();
var topValue = rect.top;
var bottomValue = rect.bottom;
return Math.max(0, topValue > 0 ? Math.min(elementHeight, windowHeight - topValue) : (bottomValue < windowHeight ? bottomValue : windowHeight));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment