From 015c54cc48c0e1051bb7903ece7527cf0b074e27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tu=C4=9F=C3=A7e=20K=C3=BC=C3=A7=C3=BCko=C4=9Flu?= Date: Wed, 28 Dec 2022 12:32:27 +0300 Subject: [PATCH] landing scroll top fix --- middleware/route.global.js | 9 +++++++++ plugins/scrollToTop.js | 23 ----------------------- 2 files changed, 9 insertions(+), 23 deletions(-) create mode 100644 middleware/route.global.js delete mode 100644 plugins/scrollToTop.js diff --git a/middleware/route.global.js b/middleware/route.global.js new file mode 100644 index 000000000..89c09ed84 --- /dev/null +++ b/middleware/route.global.js @@ -0,0 +1,9 @@ +export default defineNuxtRouteMiddleware((to, from) => { + useNuxtApp().hook('page:finish', () => { + if (to.path !== from.path && history.state.scroll) { + setTimeout(() => window.scrollTo(history.state.scroll), 0); + } else { + setTimeout(() => window.scrollTo(0, 0), 0); + } + }); +}); diff --git a/plugins/scrollToTop.js b/plugins/scrollToTop.js deleted file mode 100644 index e4977e4a3..000000000 --- a/plugins/scrollToTop.js +++ /dev/null @@ -1,23 +0,0 @@ -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); - }); - }; -});