Scrolling positions between Vue routing pages influence each other
background : page A scrolls to position 800 and jumps to page B, where page B is also at position 800 (if page B is not high enough, it is at the bottom).
A page: keepalive is true
B page: keepalive is false
case 1 : page B remains unchanged-- return-- page An is still at 800th position.
case 2 : page B scrolls to the top-- returns-- page An also goes back to the top.
I want their page scrolling positions to be independent. Ask for help from all kinds of seniors.
set the meta information of the route, control the scrolling behavior of the route, and cooperate with the scrollBehavior method to make the new routing page default to the top every time you open it.
const routes = [
{ path: '/', name:'index', component: index, meta: {x: 0, y: 0} },
{ path: '/swipe', name:'swipe', component: swipe, meta: {x: 0, y: 0} }
]
export default new VueRouter({
routes: routes,
scrollBehavior(to, from, savedPosition) {
return to.meta;
}
})
based on the meta-information of the set route meta, set the method in the home index component to remember that the scrolling location is saved to the routing meta:
mounted() {
window.onscroll = this.justifyPos;
},
methods: {
justifyPos() {
// ;
if (this.timerId) {
clearTimeout(this.timerId);
}
this.timerId = setTimeout(() => {
// meta
this.$route.meta.y = window.pageYOffset;
}, 300);
}
}
refer to https://www.jianshu.com/p/c80.
ide/advanced/scroll-behavior.html" rel=" nofollow noreferrer "> Vue Router scrolling behavior
consider using css to control scrolling
parent element setting
height:100%;
overflow:hidden;
Scroll element plus:
overflow-y:scroll;
-webkit-overflow-scrolling:touch;