给需要定位的模块设置唯一id
在需要跳转的事件中拟写
// 定义锚点跳的元素
const recommendComponent = this.selectComponent('#recommend')
const query = wx.createSelectorQuery().in(this)
query.select('#recommend').boundingClientRect()
let _this = this
query.exec(res => {
_this.scrollTopFn(res[0].top)
})
}
// 此处是页面滚动的高度
// 如果页面有自定义导航,并且该navBar吞掉页面的高度,需要将页面整体高度减去相应的导航高度
scrollTopFn(top) {
wx.pageScrollTo({ scrollTop: top - this.navbarHeight })
}
// 跨页面锚点跳
// 首先可以在元素上面挂载data参数 例如:
<!-- 主页面 -->
<image data-detail="toList"></image>
const detail = e.currentTarget.dataset.detail
// 之后就可以携带query参数跳转到对应的页面
<!-- 跳转页面 -->
可以在onLoad中取出参数作为判断条件 执行上面的代码操作就可以跳转了。(不使用scroll-view哦)