Skip to content

Instantly share code, notes, and snippets.

@matomesc
Last active October 16, 2023 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matomesc/bc0647dcb016cc5b8962a3642aad5228 to your computer and use it in GitHub Desktop.
Save matomesc/bc0647dcb016cc5b8962a3642aad5228 to your computer and use it in GitHub Desktop.
Get the maximum height an element can be to fill the viewport
/**
* Get the maximum height an element can be to fill the viewport.
*
* @param el
* @returns
*/
function getMaxHeight(el: HTMLElement) {
function pageY(elem: HTMLElement): number {
return elem.offsetParent
? elem.offsetTop + pageY(elem.offsetParent as HTMLElement)
: elem.offsetTop;
}
let height = Math.max(
document.documentElement.clientHeight || 0,
window.innerHeight || 0,
);
height -= pageY(el);
height = height < 0 ? 0 : height;
return height;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment