介绍:
要求实现,根据id自动滚动到列表中的某一条数据位置
问题:
使用ref绑定,在this. n e x t T i c k 根据 t h i s . nextTick根据this. nextTick根据this.refs.id获取元素读取其offsetTop一直为undefined,pc端测试一直正常,app端如果是在第一页,正常;如果页数较多,数据量大,在第N页中,就会导致拿不到元素offsetTop,使用setTimeout方式也无法正确读取
解决方法:
第一步:
//根据id滚动到指定位置
let obj= uni.createSelectorQuery().in(this).select('#id'+this.scrollToId)//在select('#id')中填写元素绑定的id即可
obj.boundingClientRect(data => {
//data为该元素位置信息
uni.pageScrollTo({
scrollTop: data.top - 60,
duration:10
})
}).exec()
番外篇:
2023值得铭记!!!