const query = wx.createSelectorQuery().in(that)
query
.selectAll('.item66')
.boundingClientRect(function(rect) {
let width = 0
// 循环获取计算当前点击的标签项距离左侧的距离
for (let i = 0; i < index; i++) {
width += rect[i].width
}
console.log(width)
// 当大于屏幕一半的宽度则滚动,否则就设置位置为0
let clientWidth = wx.getSystemInfoSync().windowWidth / 2
if (width > clientWidth) {
that.setData({
scrollLft: width + rect[index].width / 2 - clientWidth
})
} else {
that.setData({
scrollLft: 0
})
}
})
.exec()