Scrolltotop issue fixed for history back routes

pull/3449/head
Bahadır Sofuoğlu 2022-12-28 01:53:08 +03:00
parent 06df25c7cc
commit 66668701f8
2 changed files with 23 additions and 9 deletions

View File

@ -1,9 +0,0 @@
export default defineNuxtRouteMiddleware((to, from) => {
useNuxtApp().hook('page:finish', () => {
if (to.path !== from.path && history.state.scroll) {
window.scrollTo(history.state.scroll);
} else {
window.scrollTo(0, 0);
}
});
});

23
plugins/scrollToTop.js Normal file
View File

@ -0,0 +1,23 @@
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.$router.options.scrollBehavior = async (to, from, savedPosition) => {
let goTo;
if (to.hash) {
console.log(to);
goTo = {
el: to.hash,
top: 0
};
} else if (savedPosition) {
goTo = savedPosition;
} else {
goTo = { top: 0 };
}
return new Promise((resolve) => {
setTimeout(() => {
resolve(goTo);
}, 100);
});
};
});