Skip to content

Instantly share code, notes, and snippets.

@moaoa
Last active September 16, 2023 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moaoa/9182ec933c9d67f06bc8b27fe7ab7b32 to your computer and use it in GitHub Desktop.
Save moaoa/9182ec933c9d67f06bc8b27fe7ab7b32 to your computer and use it in GitHub Desktop.
vue_3_scrolllBehavior
export const Router = createRouter({
history: createWebHistory(),
routes,
scrollBehavior: (to, from, savedPosition) => {
if (to && to.hash) {
return { el: to.hash, behavior: 'smooth' };
} else {
return { x: 0, y: 0 };
}
},
});
//another example
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,
}
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment