Created
August 15, 2023 16:12
-
-
Save farnabaz/a87a12a6736fd595d7ab0b19afb282a1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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