mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-31 07:40:33 +00:00
fix(nuxt): prevent initial scroll jump when loading page (#25483)
This commit is contained in:
parent
995acd9983
commit
ecc4c8e0c5
@ -78,13 +78,25 @@ const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({
|
|||||||
startPosition = savedPosition
|
startPosition = savedPosition
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// reset scroll behavior to initial value
|
if (routerOptions.scrollBehavior) {
|
||||||
router.options.scrollBehavior = routerOptions.scrollBehavior
|
// reset scroll behavior to initial value
|
||||||
return routerOptions.scrollBehavior?.(to, START_LOCATION, startPosition || savedPosition)
|
router.options.scrollBehavior = routerOptions.scrollBehavior
|
||||||
|
if ('scrollRestoration' in window.history) {
|
||||||
|
const unsub = router.beforeEach(() => {
|
||||||
|
unsub()
|
||||||
|
window.history.scrollRestoration = 'manual'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return routerOptions.scrollBehavior(to, START_LOCATION, startPosition || savedPosition)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
history,
|
history,
|
||||||
routes
|
routes
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (import.meta.client && 'scrollRestoration' in window.history) {
|
||||||
|
window.history.scrollRestoration = 'auto'
|
||||||
|
}
|
||||||
nuxtApp.vueApp.use(router)
|
nuxtApp.vueApp.use(router)
|
||||||
|
|
||||||
const previousRoute = shallowRef(router.currentRoute.value)
|
const previousRoute = shallowRef(router.currentRoute.value)
|
||||||
|
Loading…
Reference in New Issue
Block a user