By default, page navigation in Nuxt.js 3 scrolls to the top.

Hi Devs, I'm facing an issue in Nuxt.js. When I scroll to the bottom of a page and navigate to another page, the scroll position resets to the top. I want the page to always start from the top, no matter where I scrolled. How can I prevent this default scroll behavior? i have tried many time but not get proper solution.
Solution:
i have fixed it - import type { RouterConfig } from "@nuxt/schema"; export default <RouterConfig>{ scrollBehavior(to, from, savedPosition) { if (savedPosition) {...
Jump to solution
4 Replies
kapa.ai
kapa.ai2mo ago
Hi @mdhaiderhossain. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- It seems there might be some confusion in your question. By default, Nuxt.js 3 actually does scroll to the top when navigating between pages, which is the behavior you're looking for. However, if you're experiencing issues with this default behavior, there are a few things we can check and adjust. 1. First, make sure you haven't accidentally disabled the default scroll-to-top behavior in your Nuxt configuration. The default scrollBehavior should scroll to the top on page navigation. 2. If you're using custom layouts or have a complex page structure, sometimes the default scroll behavior might not work as expected. In such cases, you can customize the scroll behavior using the scrollBehavior option in your router configuration. Here's how you can ensure the scroll-to-top behavior: 1. Create or modify the file ~/app/router.options.ts (or .js) and add the following code:
import type { RouterConfig } from '@nuxt/schema'

export default <RouterConfig>{
scrollBehavior(to, from, savedPosition) {
return { top: 0 }
}
}
import type { RouterConfig } from '@nuxt/schema'

export default <RouterConfig>{
scrollBehavior(to, from, savedPosition) {
return { top: 0 }
}
}
This configuration will force scrolling to the top on every navigation Custom Routing (https://nuxt.com/docs/guide/recipes/custom-routing#scroll-behavior-for-hash-links). 2. If you're still facing issues, it might be related to the timing of the scroll action. In some cases, especially with page transitions or dynamic content, you might need to delay the scroll action slightly. You can modify the scrollBehavior function like this:
export default <RouterConfig>{
scrollBehavior(to, from, savedPosition) {
return new Promise((resolve) => {
setTimeout(() => {
resolve({ top: 0 })
}, 50)
})
}
}
export default <RouterConfig>{
scrollBehavior(to, from, savedPosition) {
return new Promise((resolve) => {
setTimeout(() => {
resolve({ top: 0 })
}, 50)
})
}
}
This adds a small delay before scrolling, which can help ensure all page content is loaded before the scroll action occurs. 3. If you're using page transitions, make sure they're not interfering with the scroll behavior. You might need to adjust your transition timing or add the scroll action after the transition completes. 4. Lastly, if you're still experiencing issues, it could be related to a known bug that was fixed in recent versions of Nuxt. Make sure you're using the latest version of Nuxt 3, as several scroll-related issues have been addressed in recent updates Github PR #25817 (https://github.com/nuxt/nuxt/pull/25817). If you've tried these solutions and are still facing issues, please provide more details about your specific setup, including any custom configurations or components that might be affecting the scroll behavior. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@765806979481600021> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
mdhaiderhossain
mdhaiderhossainOP2mo ago
The project is not solved yet.
Solution
mdhaiderhossain
mdhaiderhossain2mo ago
i have fixed it - import type { RouterConfig } from "@nuxt/schema"; export default <RouterConfig>{ scrollBehavior(to, from, savedPosition) { if (savedPosition) { return savedPosition; } return { top: 0, behavior: "instant" }; }, };
Want results from more Discord servers?
Add your server