From 384e9e93ac5d46c3e437692b7805589b66b789ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bahad=C4=B1r=20Sofuo=C4=9Flu?= Date: Tue, 27 Dec 2022 11:12:25 +0300 Subject: [PATCH] Index page scroll problem fixed --- middleware/route.global.js | 9 +++++++++ pages/Intro.vue | 29 +++++++++++++++++++++++++++++ pages/index.vue | 25 ++++--------------------- plugins/scrollToTop.js | 9 --------- 4 files changed, 42 insertions(+), 30 deletions(-) create mode 100644 middleware/route.global.js create mode 100644 pages/Intro.vue delete mode 100644 plugins/scrollToTop.js diff --git a/middleware/route.global.js b/middleware/route.global.js new file mode 100644 index 000000000..d3a517024 --- /dev/null +++ b/middleware/route.global.js @@ -0,0 +1,9 @@ +export default defineNuxtRouteMiddleware((to, from) => { + useNuxtApp().hook('page:finish', () => { + if (history.state.scroll) { + setTimeout(() => window.scrollTo(history.state.scroll), 0); + } else { + setTimeout(() => window.scrollTo(0, 0), 0); + } + }); +}); diff --git a/pages/Intro.vue b/pages/Intro.vue new file mode 100644 index 000000000..034a78695 --- /dev/null +++ b/pages/Intro.vue @@ -0,0 +1,29 @@ + + + diff --git a/pages/index.vue b/pages/index.vue index 2c2a6d9e8..4120a5d05 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -1,10 +1,6 @@ diff --git a/plugins/scrollToTop.js b/plugins/scrollToTop.js deleted file mode 100644 index c69fdce68..000000000 --- a/plugins/scrollToTop.js +++ /dev/null @@ -1,9 +0,0 @@ -export default defineNuxtPlugin((nuxtApp) => { - nuxtApp.$router.options.scrollBehavior = () => { - return new Promise((resolve) => { - setTimeout(() => { - resolve({ left: 0, top: 0, behaviour: 'smooth' }); - }, 100); - }); - }; -});