Skip to content

Instantly share code, notes, and snippets.

@farnabaz
Created August 15, 2023 16:12
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 farnabaz/a87a12a6736fd595d7ab0b19afb282a1 to your computer and use it in GitHub Desktop.
Save farnabaz/a87a12a6736fd595d7ab0b19afb282a1 to your computer and use it in GitHub Desktop.
import type { RouterConfig } from '@nuxt/schema'
// https://router.vuejs.org/api/#routeroptions
export default <RouterConfig>{
scrollBehavior: (to, _from, savedPosition) => {
if (to.params?.stop) {
return
}
if (to.hash) {
const el = document.querySelector(to.hash)
// vue-router does not incorporate scroll-margin-top on its own.
if (el) {
const top = parseFloat(getComputedStyle(el).scrollMarginTop)
return {
el: to.hash,
behavior: 'smooth',
top
}
}
return {
el: to.hash,
behavior: 'smooth'
}
}
// Scroll to top of window
if (savedPosition) {
return savedPosition
} else {
return { top: 0 }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment