直接上代码:
/**
* 点击左侧导航栏
*/
slidebarChange: function(event) {
let tarId = "#index" + event.detail + "_0";
const query = wx.createSelectorQuery()
query.select(tarId).boundingClientRect()
query.selectViewport().scrollOffset()
query.exec(function(res) {
console.log(res);
let selectTop = res[0].top;
let scrollTop = res[1].scrollTop;
wx.pageScrollTo({
scrollTop: selectTop,
})
})
},
在点击同一个导航栏的时候 selectTop 和scrollTop 相互变化
如果节点在 scroll-view 中,需要这样处理:
const query = wx.createSelectorQuery() query.select('#filter_bar').boundingClientRect() query.select('#page-scroll-view').scrollOffset() query.exec(function(res){ const [rect, scrollRect] = res let top = (rect && rect.top) || 0 let scrollTop = (scrollRect && scrollRect.scrollTop) || 0 const offsetTop = scrollTop + top })
页面滚动了吧