Add smooth scroll options in blog

Signed-off-by: nurRiyad <asadnurriyad@gmail.com>
This commit is contained in:
nurRiyad
2023-11-18 02:26:32 +06:00
parent 95a27d95c9
commit 350fcd8f14
2 changed files with 39 additions and 1 deletions

38
app/router.options.ts Normal file
View File

@@ -0,0 +1,38 @@
import type { RouterConfig } from '@nuxt/schema'
// https://router.vuejs.org/api/#routeroptions
export default <RouterConfig>{
scrollBehavior: (to, from, savedPosition) => {
// scroll to hash, useful for using to="#some-id" in NuxtLink
// ex: <NuxtLink to="#top"> To Top </ NuxtLink>
if (to.hash) {
// console.log('to.hash: ', to.hash)
return {
el: to.hash,
top: 100,
behavior: 'smooth',
}
}
// The remainder is optional but maybe useful as well
// if link is to same page, scroll to top with smooth behavior
if (to === from) {
return {
left: 0,
top: 0,
behavior: 'smooth',
}
}
// this will use saved scroll position on browser forward/back navigation
return new Promise((resolve) => {
setTimeout(() => {
resolve({
left: savedPosition?.left || 0,
top: savedPosition?.top || 0,
})
}, 500)
})
},
}

View File

@@ -2,7 +2,7 @@
const { path } = useRoute()
const articles = await queryContent(path).findOne()
const links = articles.body.toc.links
const links = articles?.body?.toc?.links || []
</script>
<template>