uni-app 组件里面获取元素宽高的实现
作者:MangoGoing
这篇文章主要介绍了uni-app 组件里面获取元素宽高的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
遇到的问题:直接在组件的mounted里面创建选择器,获取元素的宽高,即使扔到定时器里面执行,还是偶尔会有获取不到的情况
解决办法:

// 写法一:
getDescBox() {
uni.createSelectorQuery().in(this).select('.top .desc').boundingClientRect(result => {
if (result) {
console.log('==========',result)
}else {
this.getDescBox();
}
}).exec();
},
// 写法二:
getDescBox() {
uni.createSelectorQuery().in(this).select('.top .desc').boundingClientRect().exec((res)=>{
if (res\[0\]) {
this.descHeight \= res\[0\].height;
}else {
this.$nextTick(()=>{
this.getDescBox();
})
}
})
},
两种写法一模一样...
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
