-
-
Save mistical2008/98d5c0adabb6e94c753645369e52837f to your computer and use it in GitHub Desktop.
Measure width and height of an unrendered HTML element.
This file contains hidden or 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
| /** | |
| * Measure an element. | |
| * | |
| * @param {HTMLElement} element - The element. | |
| * @param {HTMLElement} [parentNode] - The parent element. | |
| * @return {Object} | |
| */ | |
| function measure(element, parentNode) { | |
| parentNode = parentNode || document.body; | |
| element.style.display = 'block'; | |
| element.style.visibility = 'hidden'; | |
| element.style.zIndex = -1; | |
| parentNode.appendChild(element); | |
| const { | |
| clientWidth, | |
| clientHeight, | |
| offsetWidth, | |
| offsetHeight, | |
| scrollWidth, | |
| scrollHeight, | |
| } = element; | |
| parentNode.removeChild(element); | |
| return { | |
| clientWidth, | |
| clientHeight, | |
| offsetWidth, | |
| offsetHeight, | |
| scrollWidth, | |
| scrollHeight, | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment