微信小程序
需要看到滑动效果:
scroll-into-view:值应为某子元素id(id不能以数字开头)。设置哪个方向可滚动,则在哪个方向滚动到该元素
<scroll-view v-if="showPage" scroll-y :scroll-into-view="scrollViewId" class="member-container">
......
<div id="freight-coupon-box">
</div>
<div id="member-goods-list-box">
</div>
</scroll-view>
onLoad (option) {
if (option.toGoodsList) {
this.toGoodsList = option.toGoodsList
}
if (option.toFreight) {
this.toFreight = option.toFreight
}
},
async onShow () {
if (this.toGoodsList) {
this.scrollViewId = 'member-goods-list-box'
}
if (this.toFreight) {
this.scrollViewId = 'freight-coupon-box'
}
},
在页面中需要点击滑动,使用uni.pageScrollTo
const query = uni.createSelectorQuery().in(this)
query.select('#goods_tab').boundingClientRect(data =>{
this.goodsTabsTop = data.top
}).exec()
let top = this.goodsTabsTop + 70
uni.pageScrollTo({
scrollTop: top,
duration: 300
})
不需要看到滑动效果,直接定位:
在页面渲染之前提示加载中,不显示页面,等页面渲染之后,滚动完成再显示页面
async onShow () {
if (this.toGoodsList) {
this.scrollViewId = 'member-goods-list-box'
}
if (this.toFreight) {
this.scrollViewId = 'freight-coupon-box'
}
this.showPage = true
},
H5
不需要看到滑动效果,直接定位:
<div v-if="showPage" class="member-container" ref="memberContainer">
......
<div id="freight-coupon-box">
</div>
<div id="member-goods-list-box">
</div>
</div>
.member-container {
position: absolute;
top: 0;
bottom: 0;
overflow: scroll;
}
async init () {
await this.getUserInfo()
await this.loadGoodsList()
this.showPage = true
this.refreshing = false
if (this.toGoodsList) {
this.$nextTick(() => {
// 定位到prime专享折扣区域
document.querySelector("#member-goods-list-box").scrollIntoView(true)
this.$refs.memberContainer.scrollTop -= 15
this.toGoodsList = false
})
}
if (this.toFreight) {
this.$nextTick(() => {
// 定位到每月包邮区域
document.querySelector("#freight-coupon-box").scrollIntoView(true)
this.$refs.memberContainer.scrollTop -= 6
this.toFreight = false
})
}
},
往上滚动多少距离:
this.$refs.retailList.scrollTop -= 58