[2.0] scrollBehavior doesn't store a scrolling position on the first page
See original GitHub issueVue.js & vue-router version
2.0.2, 2.0.1
Steps to reproduce
http://jsfiddle.net/sergeymakinen/zfwhp07p/
- Click some
Go to…
links - Click
Go back
links until you’re back - Watch the browser’s console
Expected
scrollBehavior
callback should return all saved positions on all pages back to the start page.
Actually happens
Instead the callback returns all except the very first page saved positions. It looks like there should be some replaceState
call with a generated key when history
mode initializes to be able to catch the key later in popstate
.
Issue Analytics
- State:
- Created 7 years ago
- Comments:11 (1 by maintainers)
Top Results From Across the Web
[2.0] scrollBehavior doesn't store a scrolling position on the first page
[2.0] scrollBehavior doesn't store a scrolling position on the first page.
Read more >Vue.js scroll to top of new page route after setTimeout
I'd like to wait 100ms before it automatically scrolls to the top. The following code doesn't end up scrolling at all. Is there...
Read more >Scroll Behavior | Vue Router
When using client-side routing, we may want to scroll to top when navigating to a new route, or preserve the scrolling position of...
Read more >How To Implement Smooth Scrolling in React - DigitalOcean
Smooth scrolling is when instead of clicking on a button and being instantly taken to a different part of the same page, ...
Read more >scroll-behavior - CSS: Cascading Style Sheets - MDN Web Docs
The scroll-behavior CSS property sets the behavior for a scrolling box when ... overflow-y: scroll; scroll-behavior: smooth; } .scroll-page ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I read the
vue-router.js
source code, when you click browser history back button,the following things will happen:**1.**popstate event triggered,then we here:
Note that the original history entry (for http://example.com/example.html) has no state object associated with it.
So,
_key
will be null.**2.**Then, the program will run into this
handleScroll
function:**3.**There is a
$nextTick
function, so program will run into it:Because the
_key
is null, sogetScrollPosition(_key)
will return null. My config:shouldScroll
will be{x:0, y:0}
, program will runwindow.scrollTo(position.x, position.y)
, then page will scroll to top temporarily.Attention: temporarily!
Continue running the program, The page will scroll to its original location,
scrollTop(0, 100)
will be invalid! It doesn’t work like the doc said:I am very confused!
4.
vue-router
will listen ‘scroll’ event:_key
is null, won’t save position. But, when you already back to last page,your scroll behavior also trigger this,and the_key
will have a value, because:Now, I’m not sure I know how to use vue-router
scrollBehavior
.I’ve been struggling with the same problem.
When the router is initialized, the current route
R1
does not have state registered in the history (window.history.state -> null
). Let’s say thatR1
contains a long list view and the user scrolls to the bottom, so thatwindow.scrollY
becomes2500
.Now the user navigates to another route
R2
by clicking one of the list items. The scroll position ofR1
({ x: 0, y: 2500 }
) is saved to thepositionStore
(scroll.js#L7). When clicking the back button,popstate
event callback receives info aboutR1
. Since it does not have its history state registered (e.state = null
), no expected scrolling takes place. That’s where it breaks.My current workaround is to replace the history state of the current route when the router is initialized. I just added
pushState(cleanPath(this.getCurrentLocation()), true)
to theHTML5History
constructor. The critical part is to have the generated key added to the first route’s state.