用这个方法获取元素高度为0
![](https://img-blog.csdnimg.cn/img_convert/69d859d00fb5d3aee90acd15d2f2d0ff.png)
刚开始写法
onShow() {
// let top = res.height - this.scrollViewHeight;
this.$nextTick(() => {
uni.createSelectorQuery().in(this).select('#scroll').boundingClientRect((
res) => {
console.log(this.scrollViewHeight, "res", res)
if (top > 0) {}
}).exec()
})
},
出现原因:
onLoad() { //页面初始化执行,用户页面获取参数},
onReady() { //页面初次渲染完毕执行},
解决方法:
在onReady()写入这些,再加上延时器
改后的效果
![](https://img-blog.csdnimg.cn/img_convert/b3cb16f6ed6aedf9419cfdabfd8f32dc.png)
改后的代码
onReady() {
this.$nextTick(() => {
setTimeout(() => {
let infos = uni
.createSelectorQuery()
.in(this)
.select('.list');
infos
.boundingClientRect(data => {
console.log(data);
this.scrollTop = data.height
// this.setMargnTop(data.height);
})
.exec();
}, 100);
});
},