Last active
October 16, 2023 19:02
-
-
Save matomesc/bc0647dcb016cc5b8962a3642aad5228 to your computer and use it in GitHub Desktop.
Get the maximum height an element can be to fill the viewport
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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