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

scrollBehavior Glitches #1376

Closed
pi0 opened this issue Aug 14, 2017 · 58 comments
Closed

scrollBehavior Glitches #1376

pi0 opened this issue Aug 14, 2017 · 58 comments

Comments

@pi0
Copy link
Member

pi0 commented Aug 14, 2017

Hi. We have various issues reported for scrollBehavior not behaving as desired. Maybe use-cases differ but almost all of them are related to the same known issue and we are aware and plan to help fixing it. Please don't make/reopen duplicated issues as there is enough info about the problem.It just makes the process harder. As of always contributions and PRs are appreciated to both vue-router and nuxt for possible fixes.

vue-router

Issues

Reproductions

Misc

This bug report is available on Nuxt.js community (#c1219)
@pi0
Copy link
Member Author

pi0 commented Oct 12, 2017

UPDATE new Async Scrolling will be available with vue-router@2.8.0 (Thanks to @homerjam . vuejs/vue-router#1758)

@typewriter-software
Copy link

Excellent, does anyone have a good way to hook this up with the router transition object to detect when to scroll? Or should we be using a timeout like in the documentation's example.

@hackuun
Copy link

hackuun commented Oct 12, 2017

@pi0 is there's a hope that it will fix ScrollToTop page transitions?

@homerjam
Copy link

@typewriter-software check out updated example here vuejs/vue-router#1804 we still need a small modification to vue-router for this to work (I wish I had included this in my last PR!).

@Atinux
Copy link
Member

Atinux commented Nov 2, 2017

#1376

@hackuun
Copy link

hackuun commented Nov 7, 2017

Any changes? How to use this promise?

@homerjam
Copy link

homerjam commented Nov 7, 2017

@iamdubx still waiting for the PR referenced above to be released, then I can make an example. For now you should be able to get it to work by installing vue-router from github and using the afterLeave hook as in the example in the PR (the afterLeave hook goes in nuxt.config.js).

@hackuun
Copy link

hackuun commented Nov 8, 2017

@homerjam good to hear, that it's not only me concern this issue.
Meanwhile, I found this awesome project, and wonder, how it fix Scroll behavior? I can't find anything regarding this issue in source code - https://github.com/krestaino/nuepress
Any ideas how it was done?

@homerjam
Copy link

homerjam commented Nov 8, 2017

It looks to me like it's using the default scroll behaviour but the transitions are so quick you don't see any issues.

@krestaino
Copy link

@iamdubx Thanks! I've been eyeing this issue for some time now, once this issue is resolved in Nuxt I'll update NuePress with the fix.

@homerjam I am suffering from this scroll issue actually. It just appears sporadically as far as I've noticed.

@husayt
Copy link
Contributor

husayt commented Nov 11, 2017

I also can't wait for this fix. Scrollbehavior doesn't work at all for us, even scrollBehavior function is not being called at all. I am using spa config with hash router mode.

@dan-gamble
Copy link

I don't want to start a new thread so going to pop it in here, does this include an issue of pages not scrolling to the top and just snapping there? I seem to have that issue on nuxt examples and a clean install in all browsers i try.

@krestaino
Copy link

@dan-gamble Is this relevant at all to your issue?

https://nuxtjs.org/api/pages-scrolltotop/

@dan-gamble
Copy link

That seems to break my page weirdly enough, it also still snaps to the top for some reason.

@dan-gamble
Copy link

dan-gamble commented Jan 11, 2018

Found out i needed:

    watchQuery: ['page'],
    key: (to) => to.fullPath,

To get me working again, scrollToTop still snaps up for me though. I'll get a video to show what i get.

Edit
video

@jkole
Copy link

jkole commented Jan 18, 2018

Is there any status update on all this? Vue, Nuxt, etc are absolutely fantastic, but the bizarre scrolling to top before route transitions makes the ecosystem pretty much unusable for us. Most of our pages are image-intensive, with large images at the top of each page. With that sort of UI, the scrolling to top before every transition is very noticeable (with or without transition animation) and makes for a terrible user experience. I'm happy to start digging into the source code to try to figure out a workaround, but many others have already tried and it sounds like this is already an area of focus for the core contributors so I'd likely be wasting my time. Unfortunately we're at (or well past) the point where my team needs to make a final decision on React vs. Vue, and this is a critical issue so any status update would be greatly appreciated!

@homerjam
Copy link

homerjam commented Jan 18, 2018 via email

@mcrider
Copy link

mcrider commented Jan 18, 2018

@homerjam an example would be awesome -- I'm just wrapping up my nuxt project and would really like to get this resolved before launch.

@homerjam
Copy link

homerjam commented Jan 18, 2018 via email

@krestaino
Copy link

@homerjam An example would be incredibly helpful. For my specific use case, the scroll position does not return to where it was when navigating back. You can see a minimal repo example on this issue.

#1182

@mcrider
Copy link

mcrider commented Jan 18, 2018

@krestaino I think your issue is different than what is discussed here (but probably related).

I think the behavior people here are seeing (and don't want) is:

  • User is scrolled down on Page A
  • User clicks on link to Page B
  • Page A scrolls to the top before loading Page B

The router should behave like a normal website, where clicking a link does not scroll to the top of the page.

@homerjam
Copy link

homerjam commented Jan 19, 2018

There's a good explanation here. Basically would allow us to take total control of scroll position, currently there is still a clash between the browsers behavior and the use of savedPosition - specifically when using the back button.

@mcrider
Copy link

mcrider commented Jan 19, 2018

Hey hey I think we're good here! You rock @homerjam :)

I did add window.history.scrollRestoration = 'manual' to the nuxt.config.js::scrollBehavior method, otherwise when hitting the back button the originating page would jump down before transitioning.

One slight problem, I have a subpage (like your /about/contact, except its dynamic, e.g. /work/_project.vue) where I have scrollToTop set. Doesn't seem to do anything :( When I navigate from the parent (work) page to the child (project) page it loads the child at the scroll position you left the parent. Is else if (to.matched.some((r) => r.components.default.options.scrollToTop)) valid for a dynamic subpage? I'm having trouble trying to debug this config file so I can inspect variables, so not sure if this is correct.

@homerjam
Copy link

Hmm. Easiest way to find out would probably be to console.log the to state and have a look in there. Could you post a minimal example or tweak one of the ones on the docs site - I think they use glitch?

@mcrider
Copy link

mcrider commented Jan 19, 2018

Yup just realized after my last comment that console.log outputs to the browser not the terminal. The behavior is pretty inconsistent, I'll try to repro using your example.

@mcrider
Copy link

mcrider commented Jan 19, 2018

I think I just had a weird setup with my nested routes. All good now, thanks again!

@Atinux Atinux closed this as completed in 358e2f3 Jan 31, 2018
@Atinux
Copy link
Member

Atinux commented Jan 31, 2018

I just implemented the new scrollBehavior with window.history.scrollRestoration, and it works like a charm, thank you everyone to your big help on this difficult issue, big thank you to @homerjam who made a fix to vue-router!

I'm waiting for the test to be all green before pushing the v1.3.0!

@hackuun
Copy link

hackuun commented Jan 31, 2018

It's like a dream come true, Thank you guys!

@hackuun
Copy link

hackuun commented Jan 31, 2018

@Atinux is it documented? I don't see it yet. How to implement this fix? https://nuxtjs.org/api/configuration-router#scrollBehavior

@chasebank
Copy link

Is this fully implemented? I'm having a tough time with scroll positions. I think this may be because I'm setting the __nuxt element to position fixed and then scrolling it's overflow. Is there any way to tell nuxt/vue which element is scrolling?

https://glitch.com/edit/#!/brainy-coffee

@homerjam
Copy link

@chasebank you'll probably need a custom scrollBehavior() function targeting the __nuxt element, you can find the default here - overwrite it in nuxt.config.js.

@bovas85
Copy link

bovas85 commented Mar 1, 2018

I am on 1.3.0 and on a nested route ( /journal/_blog/) and it doesn't scroll to top on those if I access the route from the footer of a page, it will keep the same position I clicked from

@dohomi
Copy link

dohomi commented Mar 14, 2018

@Atinux I was trying to use scrollBehaviour on Nuxt 1.4 but it does not persist scroll position on historyBack()

I figured out that the triggerScroll seems to be fired too early, the page/component isn't loaded yet. I solved it for now with a timeout but not sure if thats a good way.

setTimeout(() => {
          resolve(position)
},200)

@daspete
Copy link

daspete commented Mar 30, 2018

@dohomi maybe better make a nextTick call?

@dohomi
Copy link

dohomi commented Mar 31, 2018

@daspete I tried but didn't work out so far.

@Atinux
Copy link
Member

Atinux commented Mar 31, 2018

Actually we call the triggerScroll event on the beforeEnter transition hook: lib/app/components/nuxt-child.js#L35

We need more investigation and test on this to be sure it works perfectly. This is dur to the lack of vue-router support on transitions and scroll position :/

@robinscholz
Copy link

Currently working on an app that glitches when two different layouts are used.

Page A and B use layout 1.
Page B and C use layout 2.

From A to B or B to C and vice versa scrolls to the top fine. However if I scroll down page A and navigate to page C for example, the page loads somewhere in the middle.

Resolved for now by using only one layout for all pages, but worth mentioning!

@homerjam
Copy link

homerjam commented Apr 7, 2018 via email

@robinscholz
Copy link

After cleaning my dependencies, removing node_modules and installing again the bug is gone. I think it was most likely caused by nuxt not being the latest version (1.4).

@robinscholz
Copy link

robinscholz commented Apr 11, 2018

I've noticed that the bug persists in Safari in my app. Anyone else have any problems with this?

The pages scroll to the top before switching to the new one. Plus the savedPosition seems to be off when navigating with Safari’s back and forward buttons (only on an image heavy page).

Wrapping resolve(position) in setTimeout fixes the issue somewhat, but the jumps are still noticable.

setTimeout(function() {
  resolve(position)
}, 10)

@robinscholz
Copy link

@Atinux @homerjam Any idea for a workaround for Safari?

@homerjam
Copy link

@studioscholz could you provide an isolated example of the issue (glitch etc)?

@robinscholz
Copy link

robinscholz commented Apr 17, 2018

@homerjam

I'll try and provide a repo when I have some time. In the meantime the bug can be observed here: https://www.soccochico.com/catalogue

To toggle it, scroll down the page to one of the very last books and click it. Then navigate back via Safari’s back button. You'll end up somewhere in the middle of the catalogue page. My best guess is that the scrollPosition is resolved before the page has gotten its real height due to the pictures being loaded.

There is also a jump visible when navigating from catalogue to privacy policy for example via the footer.

@homerjam
Copy link

@studioscholz

It's remembering the scroll position for me which is the desired behaviour! You might want to add a transition to smooth out the catalogue/privacy policy switch...

@robinscholz
Copy link

@homerjam

Weird, the bug is consistent for me with Safari: https://streamable.com/yygsg
I've noticed that if I compute the image height via the mounted hook, it solves the problem. Bit of a nasty workaround though.

@ImreC
Copy link

ImreC commented Aug 13, 2018

@dohomi @Atinux any progress or workarounds on this issue? Also experiencing issues with historyBack(). Have been at this for days and trying to use the setTimeout solution now, but this creates clearly visible jumps on the page, so definitely doesn't look great. Very open to solutions.

@dohomi
Copy link

dohomi commented Aug 14, 2018

@ImreC I still use the workaround with the timeout I showed above. Works fine for me atm

@lock
Copy link

lock bot commented Nov 1, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Nov 1, 2018
@danielroe danielroe added the 2.x label Jan 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests