Skip to content

Instantly share code, notes, and snippets.

@ejfox
Created May 13, 2022 13:38
Show Gist options
  • Save ejfox/4433ddc1e66322b48a24a4b811551d49 to your computer and use it in GitHub Desktop.
Save ejfox/4433ddc1e66322b48a24a4b811551d49 to your computer and use it in GitHub Desktop.
Vue method to set width and height based on component element size
setWidthAndHeight() {
const cs = getComputedStyle(this.$el)
const paddingX = parseFloat(cs.paddingLeft) + parseFloat(cs.paddingRight)
const paddingY = parseFloat(cs.paddingTop) + parseFloat(cs.paddingBottom)
const borderX =
parseFloat(cs.borderLeftWidth) + parseFloat(cs.borderRightWidth)
const borderY =
parseFloat(cs.borderTopWidth) + parseFloat(cs.borderBottomWidth)
// Element width and height minus padding and border
this.width = this.$el.offsetWidth - paddingX - borderX
this.height = this.$el.offsetHeight - paddingY - borderY
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment