Skip to content

Instantly share code, notes, and snippets.

@mistical2008
Forked from remarkablemark/measure.js
Created March 21, 2018 06:46
Show Gist options
  • Select an option

  • Save mistical2008/98d5c0adabb6e94c753645369e52837f to your computer and use it in GitHub Desktop.

Select an option

Save mistical2008/98d5c0adabb6e94c753645369e52837f to your computer and use it in GitHub Desktop.
Measure width and height of an unrendered HTML element.
/**
* 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