Business needs
The page automatically scrolls to the top
Implementation plan
1. The overall page scrolls to the top
uni.pageScrollTo({
scrollTop: 0,
duration: 300
});
2. The content in the < scroll-view/ > tag on the page scrolls to the top
Different from the first solution, this is for the content in the scroll-view tag, while uni.pageScrollTo is for the entire page
Documentation: https://uniapp.dcloud.net.cn/tutorial/vue-api.html#%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
// 监听scroll事件,记录组件内部变化的值,在设置新值之前先设置为记录的当前值
<scroll-view scroll-y="true" :scroll-top="scrollTop" @scroll="scroll"></scroll-view>
export default {
data() {
return {
scrollTop: 0,
old: {
scrollTop: 0
}
}
},
methods: {
scroll: function(e) {
this.old.scrollTop = e.detail.scrollTop
},
goTop: function(e) {
this.scrollTop = this.old.scrollTop
this.$nextTick(function() {
this.scrollTop = 0
});
}
}
}