小程序 boundingClientRect rect 为 null

文章目录
  1. 1. 代码如下
  2. 2. 原因分析
  3. 3. 解决方法

页面内定位到具体的位置,使用id的值进行查找来实现,结果出现rect为 null情况

代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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首位不能是数字,要不然小程序获取不到相关信息++

解决方法

1
<view class='physiology_text' id='slide{{items.id}}'></view>

id 前增加字母即可

评论