实现小程序,通过点击事件跳转到页面指定位置,uni.createSelectorQuery与uni.pageScrollTo结合使用来实现
uni.createSelectorQuery官方文档传送门
uni.pagescrollto官方文档传送门
stickyClick(e) {
console.log(e)
let className = ''
if (e.id == 0) {
className = '.swiper-box'
}
if (e.id == 1) {
className = '.detail-img'
}
if (e.id == 2) {
className = '.gomore-goods'
}
uni.createSelectorQuery().select(className).boundingClientRect(data => { //目标位置的节点:类或者id
uni.createSelectorQuery().select(".detail-cantent").boundingClientRect(
res => { //最外层盒子的节点:类或者id
uni.pageScrollTo({
duration: 100, //过渡时间
scrollTop: data.top - res.top - 40, //到达距离顶部的top值
})
}).exec()
}).exec();
},