Enhance scrolling behavior #111
Reference in New Issue
Block a user
Delete Branch "fix/scroll"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Save scroll position with history
Save scroll position when navigating away from a page, and restore it when navigating back to that page.
Fixes #30.
Scroll to element position when URL contains a hash
When the URL contains a hash, try to find the corresponding element, and if found, scroll to its position.
Based on darylteo/pjax@4893a2a657
Fixes #22.
Wow, nice improvement!
I think you need to reset
scrollPosto be[0, 0]rather than just0, so that you're always storing the same type of data against this key.@@ -220,3 +243,3 @@if (state.options.history) {if (this.firstrun) {if (!window.history.state) {this.lastUid = this.maxUid = newUid()This also should be
[0, 0], per my other comment@@ -236,3 +259,3 @@url: state.href,title: state.options.title,uid: this.maxUiduid: this.maxUid,This will only run if
state.options.historyis falsy, which I don't think is what you intended?Perhaps something like
instead?
This would require the default value of
this.options.scrollToto change totrueas well -- currently it's set to0if one is not provided by the user, meaning that the window will always be scrolled back to the top. If we didn't want to break backwards compatibility, then we could accept'auto'as a value for the option to trigger scroll restoration, but it's probably a behavior that users would expect and prefer to happen -- what do you think?Good point.
@@ -220,3 +243,3 @@if (state.options.history) {if (this.firstrun) {if (!window.history.state) {this.lastUid = this.maxUid = newUid()Good point.
@@ -236,3 +259,3 @@url: state.href,title: state.options.title,uid: this.maxUiduid: this.maxUid,No, I believe my code is correct.
options.historyis true when we're navigating to a page, and false when the initiator waspopstate. This is because you don't want to callpushState()when you're in middle of apopstateevent.So we only want to use the scroll position stored in history when we're responding to a
popstateevent, in which caseoptions.historyis false. Otherwise, when it's true, we want to either use the hash, orscrollTo.See earlier in #46, where we decided the default when clicking a link should be to scroll to the top.
@@ -236,3 +259,3 @@url: state.href,title: state.options.title,uid: this.maxUiduid: this.maxUid,Oh dear, that's a comprehension fail on my part, sorry! You are correct.
One thing that would be good to change though is to only restore the scroll position on popstate if
this.options.scrollTois notfalse-- another thing from #46 was a decision to let developers to decide on what is the best UX for their app, so we should make sure that this scroll restoration can be opted out of, don't you think? This matches the current behaviour when switching.@@ -236,3 +259,3 @@url: state.href,title: state.options.title,uid: this.maxUiduid: this.maxUid,options.scrollTois only documented as taking anumbervalue. From the README:I'm not even sure why the check for
options.scrollTo !== falseexists at all.I think we should add a
scrollRestorationoption instead, similar to the HTML spec (see #40).@@ -236,3 +259,3 @@url: state.href,title: state.options.title,uid: this.maxUiduid: this.maxUid,Yes, I wondered why it hadn't been documented.
Adding a
scrollRestorationoption sounds fine though.@robinnorth Are we good to merge this?
LGTM. Nice one!