在 router —> index.js
// 在 router —> index.js
// 效果说明:点击这个路由后,往下滚动页面(滚动页面隐藏顶部的一部分),再点击路由:/login , 页面保持原先的滚动位置 ; 再点击这个路由, 页面会滚动到顶部,因为配置了scrollToTop为true
const scrollBehavior (to, from, savedPosition) {
if (savedPosition) { // 如果没配置scrollToTop或配置scrollToTop为false, 点击这个路由,页面保持原先的滚动位置
return savedPosition;
} else {
const position = {};
if (to.hash) {
position.selector: to.hash;
}
// 如果路由里面配置了scrollToTop为true,点击这个路由,页面会滚到顶部
if(to.matched.some(route => route.meta.scrollToTop )){
position.x = 0;
position.y = 0;
}
return position;
}
}
export default new Router({
mode: "history",
scrollBehavior,
routes
});
下方是我微信公众号的二维码,可以扫码关注以下,后期博文推送主要在公众号上面,有什么问题也可以通过公众号跟我发消息哦~