700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > vue获取dom元素的宽高

vue获取dom元素的宽高

时间:2019-05-25 06:48:59

相关推荐

vue获取dom元素的宽高

html部分:

<div ref="menuDiv"></div>

js部分:

1.offsetWidthoffsetHeight,返回Number类型,例如:200

let domWidth =this.$refs.menuDiv.offsetWidth; //宽度

let domHeight =this.$refs.menuDiv.offsetHeight;//高度

console.log(domWidth,domHeight)//打印dom的宽度,高度

2.window对象的getComputedStyleAPI,返回String类型,包含px单位,例如:"200px"。

let domWidth =window.getComputedStyle(this.$refs.menuDiv).width; //宽度

let domHeight =window.getComputedStyle(this.$refs.menuDiv).height;//高度

console.log(domWidth,domHeight)//打印dom的宽度,高度

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。