Skip to content

Instantly share code, notes, and snippets.

@1natsu172
Created February 1, 2019 10:08
Show Gist options
  • Save 1natsu172/35965df83e4ab5915489285e9845d327 to your computer and use it in GitHub Desktop.
Save 1natsu172/35965df83e4ab5915489285e9845d327 to your computer and use it in GitHub Desktop.
dom elementのRectを取って計算して返すくん。paddingを含まない値も欲しくてとりあえず書いたけど、ボツになったので供養
export const getElementRects = (element: HTMLElement) => {
const heightWithPadding = element.getBoundingClientRect().height
const heightWithoutPadding = () => {
if (!getComputedStyle) return 0
const { paddingTop, paddingBottom } = getComputedStyle(element)
const padding =
parseFloat(paddingTop ? paddingTop : '0') +
parseFloat(paddingBottom ? paddingBottom : '0')
return heightWithPadding - padding
}
return {
heightWithPadding,
heightWithoutPadding: heightWithoutPadding()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment