使用前端路由,当切换到新路由时,想要页面滚到顶部,或者是保持原先的滚动位置,就像从新加载页面那样。 vue-router
能作到,并且更好,它让你能够自定义路由切换时页面如何滚动。html
注意: 这个功能只在 HTML5 history 模式下可用。前端
当建立一个 Router 实例,你能够提供一个 scrollBehavior
方法:vue
const router = new VueRouter({ routes: [...], scrollBehavior (to, from, savedPosition) { // return 指望滚动到哪一个的位置 } })
scrollBehavior
方法接收 to
和 from
路由对象。第三个参数 savedPosition
当且仅当 popstate
导航 (经过浏览器的 前进/后退 按钮触发) 时才可用。vue-router
这个方法返回滚动位置的对象信息,长这样:浏览器
{ x: number, y: number }
{ selector: string, offset? : { x: number, y: number }}
(offset 只在 2.6.0+ 支持)若是返回一个 falsy (译者注:falsy 不是 false
,参考这里)的值,或者是一个空对象,那么不会发生滚动。ide
举例:spa
scrollBehavior (to, from, savedPosition) { return { x: 0, y: 0 } }
对于全部路由导航,简单地让页面滚动到顶部。code
返回 savedPosition
,在按下 后退/前进 按钮时,就会像浏览器的原生表现那样:router
scrollBehavior (to, from, savedPosition) { if (savedPosition) { return savedPosition } else { return { x: 0, y: 0 } } }
若是你要模拟『滚动到锚点』的行为:htm
scrollBehavior (to, from, savedPosition) { if (to.hash) { return { selector: to.hash } } }
咱们还能够利用路由元信息更细颗粒度地控制滚动。查看完整例子:
const scrollBehavior = (to, from, savedPosition) => { if (savedPosition) { // savedPosition is only available for popstate navigations. return savedPosition } else { const position = {} // new navigation. // scroll to anchor by returning the selector if (to.hash) { position.selector = to.hash } // 若是meta中有scrollTop if (to.matched.some(m => m.meta.scrollToTop)) { // cords will be used if no selector is provided, // or if the selector didn't match any element. position.x = 0 position.y = 0 } // if the returned position is falsy or an empty object, // will retain current scroll position. return position } }
与keepAlive结合,若是keepAlive的话,保存停留的位置:
scrollBehavior (to, from, savedPosition) { if (savedPosition) { return savedPosition } else { if (from.meta.keepAlive) { from.meta.savedPosition = document.body.scrollTop; } return { x: 0, y: to.meta.savedPosition ||0} } }