Skip to content
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

different default scrollBehavior that resets page scroll on navigation #13284

Closed
fanckush opened this issue Jan 19, 2022 · 14 comments
Closed

different default scrollBehavior that resets page scroll on navigation #13284

fanckush opened this issue Jan 19, 2022 · 14 comments

Comments

@fanckush
Copy link

fanckush commented Jan 19, 2022

Environment

- Operating System: `Darwin`
- Node Version:     `v17.3.1`
- Nuxt Version:     `3.0.0-27369360.33ebb01`
- Package Manager:  `yarn@1.22.17`
- Bundler:          `Vite`
- User Config:      `vite`, `ssr`
- Runtime Modules:  `-`
- Build Modules:    `-`

Reproduction

No reproduction needed imo

Describe the bug

When navigating using a <NuxtLink to="/othePage" /> the page scroll is not resetting.
In other words, the page does not scrollToTop on route change

Additional context

No response

Logs

No response

@fanckush fanckush changed the title Nuxt-Link Scroll not Resetting NuxtLink Scroll not Resetting Jan 19, 2022
@rnotorni
Copy link

It may be solved by installing a plugin for router.

#12732

@fanckush
Copy link
Author

@K-Honkawa thanks! i'll try that but the default behaviour should be to reset the scroll so i'll keep this open as a bug

@rnotorni
Copy link

rnotorni commented Jan 23, 2022

I don't think this is a Nuxt3 issue as NuxtLink is RouterLink in vue-router and the behavior that doesn't scroll to top is the default behavior of vue-rouer.

https://github.com/nuxt/framework/blob/c80870ad8c4a1c1b044d7a5ae27e41e997f8a75f/packages/nuxt3/src/pages/runtime/router.ts#L20

@fanckush
Copy link
Author

fanckush commented Jan 24, 2022

That could be the case but it is possible that NuxtLink extends RouterLink and overrides its behaviour

@danielroe danielroe changed the title NuxtLink Scroll not Resetting different default scrollBehavior that resets page scroll on navigation Jan 24, 2022
@daver987
Copy link

Any news on this issue, or is there a quick fix to get pages to scroll to top on page change. :)

@danielroe
Copy link
Member

@daver987 See above: #13284

@daver987
Copy link

daver987 commented Jan 31, 2022 via email

@jamaluddinrumi
Copy link

and in case you wanna add a bit delay, you can check it out

https://github.com/nuxt/framework/discussions/1661#discussioncomment-2159828

@danielroe
Copy link
Member

This is now configurable with nuxt/framework#3485. See https://v3.nuxtjs.org/docs/directory-structure/pages#router-options for documentation.

@fanckush
Copy link
Author

@danielroe awesome! this should replace the workaround mentioned above #13284
but if my understanding is correct it doesn't fix the default behavior which is originally described in this issue. If that's the case then I won't close this just yet

@Bodokh
Copy link

Bodokh commented Mar 28, 2022

I tried adding a plugin do change the scroll behavior like so:
export default defineNuxtPlugin((nuxtApp) => { nuxtApp.$router.options.scrollBehavior = () => { return { left: 0, top: 0 } } });

Works when running "nuxi dev"
But when running using "nuxi build" and "nuxi preview" It does not work.

@richgcook
Copy link

What is slightly undesirable about the router.options.ts option is that when using page transitions, you see the scroll to top on the current page before the current page fades out, for example. I'm wondering if there's an option to connect this with specific page transitions so it jumps to the top of the page as the current page has faded out but but the new page fades in.

export const usePageTransitionConfig = {
	mode: 'out-in',
	appear: true,
	css: false,
	onEnter(el, done) {
		loaderStore.value.isActive = false
		gsap.fromTo(el, {
			autoAlpha: 0,
			duration: 0.5
		}, {
			autoAlpha: 1,
			duration: 0.5,
			onComplete: done
		})
	},
	onLeave(el, done) {
		loaderStore.value.isActive = true
		gsap.fromTo(el, {
			autoAlpha: 1,
			duration: 0.5
		}, {
			autoAlpha: 0,
			duration: 0.5,
			onComplete: done
		})
	}
}

@fanckush
Copy link
Author

heads up, the PR nuxt/framework#3851 just got merged 🎉

@danielroe
Copy link
Member

This should be resolved in the edge channel, or in the next RC.

Let me know if not and I'll reopen.

@danielroe danielroe added the 3.x label Jan 19, 2023
@danielroe danielroe transferred this issue from nuxt/framework Jan 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants