跳到主要内容

Scroll Behavior

When using client-side routing, we may want to scroll to top when navigating to a new route, or preserve the scrolling position of history entries just like real page reload does

const router = createRouter({
scrollBehavior(to, from, savedPosition) {
if (savedPosition) {
return savedPosition
} else {
return { top: 0 }
}
},
})

const router = createRouter({
scrollBehavior(to, from, savedPosition) {
// always scroll 10px above the element #main
return {
// could also be
// el: document.getElementById('main'),
el: '#main',
top: -10,
}
},
})

const router = createRouter({
scrollBehavior(to, from, savedPosition) {
if (to.hash) {
return {
el: to.hash,
behavior: 'smooth',
}
}
}
})