New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
configurable scrollBehavior
#12732
Comments
scrollBehavior
You can set this in a plugin: export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.$router.options.scrollBehavior = () => {
return { left: 0, top: 0 }
}
}) |
@danielroe Thank you! |
This fix works not so smooth. White blink spoils the impression. |
I'm sorry. |
The plugin solution is not working smooth. Any idea when will it be fixed? |
Another thing I noticed is if you add anything with router.push it will scroll to top, even though it may not be what you want to do. router.push({
query: {
...route.query,
equipment: reAddedEquipment.map((x) => x.id)
}
}) |
There is a PR in progress: nuxt/framework#3485 |
This is now possible with nuxt/framework#3485. See https://v3.nuxtjs.org/docs/directory-structure/pages#router-options for documentation. |
Configuration has been attempted but not effective. Has anyone succeeded? |
Yes @li9269391 // app/router.options.js
export default {
scrollBehavior() {
return { top: 0 }
},
} |
@KevinNTH |
@Devaiser yes, that's it for me. I just need to extend the nuxt3 version: |
If, like me, you were not able to get this working. The problem was that I was placing the It would be nice to have a full example of this in the docs, as right now the code is spread across issues. |
@TJacoob I saw it is actually mentioned in the docs: |
As already mentioned above, this setting works rather strangely, the page first scrolls up, and only then switches to a new route |
It's working |
I saw that too but didn't think too much of it. The I understand that the docs are improving as we speak, this is meant to help others that might be making the same mistake I did. |
The transition is not smooth, the page flies up, then the transition is made. (( |
For Nuxt 3 RC 4 that I use, return { left: 0, top: 0 } not return { x: 0, y: 0 } |
this might work no need to configure
|
This solution works for me, but I experience an error in the following scenario. I have a navigation containing links to anchors on a homepage and a single link to a different page. If the homepage was rendered at least once, it works perfectly when navigating from other pages. Now, if i first load any page other than the homepage, when clicking on the nav links to the anchors on the homepage, it fails because the element it tries to find returns null, thus having no offsetTop and causing an exception. Do I need to configure anything else to account for that? |
I have just found out that if I increase the timeout from 100 to 300, it gives it enough time so that it can properly find it. Definitely not an elegant solution, but it works for now. |
Same problem, we need to increase the timeout from 100 to 300 ! |
I could use the delay too. The documented approach here: https://nuxtjs.org/docs/configuration-glossary/configuration-router#scrollbehavior did not work for me. The only thing that worked was the above solution, with naming the file But there is still that flash of the current page scrolling up first, really quick, and then it goes to the new page. |
Yes, please reopen this. The scroll behavior is very jarring. On click it immediately jumps to the top, and then the new page loads. The correct behavior should be seamless. When the new page renders, it should be scrolled to the correct position. |
@alexanderhorner This issue is closed because it is implemented (it is configurable). There are several other open issues that describe what you want. |
The nuxt3 docs/guide make no mention of in vue 2.x all i would need to do in router.js would be : this would scroll the next route to the top (like most ssr apps) without any jarring animations or flicker. The current way mentioned here will scroll to top of the current page and then shift to the next route. (rc11.) Admittedly this is a Vue router issue, but Nuxt is a "intuitive framework" so lets be a bit more of that. The only options that worked but still not the way it is 'supposed to be ' is to use a plugin with the
This will load the next route, and then scroll up (also kinda strange, but better) Nuxt3 needs a cookbook with solutions for these basic issues. |
This is actually a vue-router issue. But since Nuxt is amazing and If you have solved this issue in nuxt3, please do share your reproduction.. all my tests either have a visible page scroll up before or after the route change.. which is not what most people want. |
Hi, i tried this command and it doesnt work. Please export default defineNuxtPlugin((nuxtApp) => { I am changing page with nuxtlink but when i change page scroll stays same position "nuxt": "3.0.0-rc.11", |
Do it the official way: like this . |
@alexanderhorner thank you so much you made my day. |
Solution for Nuxt3 (RC):
|
This - scrolls to top BEFORE route navigation, hence its better to remove smooth behaviour here. |
Issue I'm having with this is when I navigate to another page from a link in the footer, the current page first scrolls to the top, then navigates away. @ricky11 As you're saying. Is there a way to make sure this happens after the route change? |
Update to the newest version. Delete any scrollBehavior config. The new default behavior works as expected. |
@alexanderhorner The only thing missing now is smooth scroll behaviour for anchor tags, do you know what needs to be edited in the router.options.ts file, to achieve this? Without breaking default page behaviour |
Have a look here: #22663 |
adding |
This isn't necessary anymore. Delete your routers option file and update to the newest version of nuxt. |
@alexanderhorner - can you please clarify what you meant by "This isn't necessary any more" and about deleting the options file? If we want to customise router options, we still supply that |
Sorry, I assumed this was to fix an older issue where nuxt didn't scroll to the top of the page on navigation. The default behavior is now smarter than most work arounds using this config. If you still want to customize the scroll behavior, this is of course valid. Edit to clarify: The default behavior is to scroll to the top of the page upon navigation, except when a position is saved (on navigating backwards for example). Probably best not to mess with that unless you have a good reason for it. |
Environment
Darwin
v16.11.0
3.0.0-27267528.292b524
npm@8.0.0
Vite
buildModules
,css
,meta
-
nuxt-windicss@^2.0.3
Describe the bug
scrollBehavior
setting (which is available in Nuxt2) doesn't work.I tried both nuxt.config.ts
router.scrollBehavior
and puttingrouter.scrollBehavior.js
in project route but they have no effect.I couldn't find any document on scrollBehavior at Nuxt3 docs website... Is there any way to make it work?
Reproduction
nuxt.config.ts
~/router.scrollBehavior.js
Additional context
No response
Logs
No response
The text was updated successfully, but these errors were encountered: