Created
May 13, 2022 13:38
-
-
Save ejfox/4433ddc1e66322b48a24a4b811551d49 to your computer and use it in GitHub Desktop.
Vue method to set width and height based on component element size
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
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