页面内定位到具体的位置,使用id的值进行查找来实现,结果出现rect为 null情况,经过检查发现,id首位不能是数字,要不然小程序获取不到相关信息,需要字母开头。
代码如下
changeId: function (e) {//监听页面滚动
console.log(e)
let id = e.currentTarget.dataset.opd;
this.setData({
toView: id,
});
console.log("----toView:" + this.data.toView);
wx.createSelectorQuery().select('#' + this.data.toView).boundingClientRect(function (rect) {
console.log("----rect:" + rect);
wx.pageScrollTo({
scrollTop: rect.top,
duration: 300
})
}).exec()
}
原因分析
++id首位不能是数字,要不然小程序获取不到相关信息++
解决方法
<view class='physiology_text' id='slide{{items.id}}'></view>
id 前增加字母即可