lunasqu.ee-nuxt/plugins/scroll-top.ts

26 lines
520 B
TypeScript
Raw Normal View History

2022-10-16 14:42:41 +00:00
import { defineNuxtPlugin } from '#imports';
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.$router.options.scrollBehavior = async (to, from, savedPosition) => {
let goTo;
if (to.hash) {
goTo = {
el: to.hash,
behavior: 'smooth',
top: 64,
};
} else if (savedPosition) {
goTo = savedPosition;
} else {
goTo = { top: 0 };
}
return new Promise((resolve) => {
setTimeout(() => {
resolve(goTo);
}, 100);
});
};
});