@@ -4,6 +4,13 @@ import type VueRouter from '../index'
44import { assert } from '../util/warn'
55import { cleanPath } from '../util/path'
66import { History } from './base'
7+ import {
8+ saveScrollPosition ,
9+ getScrollPosition ,
10+ isValidPosition ,
11+ normalizePosotion ,
12+ getElementPosition
13+ } from '../util/scroll-position'
714
815const genKey = ( ) => String ( Date . now ( ) )
916let _key : string = genKey ( )
@@ -31,7 +38,9 @@ export class HTML5History extends History {
3138 } )
3239
3340 if ( expectScroll ) {
34- window . addEventListener ( 'scroll' , saveScrollPosition )
41+ window . addEventListener ( 'scroll' , ( ) => {
42+ saveScrollPosition ( _key )
43+ } )
3544 }
3645 }
3746
@@ -69,30 +78,25 @@ export class HTML5History extends History {
6978 if ( ! behavior ) {
7079 return
7180 }
72-
7381 assert ( typeof behavior === 'function' , `scrollBehavior must be a function` )
7482
75- let position = getScrollPosition ( )
76- const shouldScroll = behavior ( to , from , isPop ? position : null )
77- if ( ! shouldScroll ) {
78- return
79- }
80-
8183 // wait until re-render finishes before scrolling
8284 router . app . $nextTick ( ( ) => {
85+ let position = getScrollPosition ( _key )
86+ const shouldScroll = behavior ( to , from , isPop ? position : null )
87+ if ( ! shouldScroll ) {
88+ return
89+ }
8390 const isObject = typeof shouldScroll === 'object'
84- if ( isObject && shouldScroll . x != null && shouldScroll . y != null ) {
85- position = shouldScroll
86- } else if ( isObject && shouldScroll . anchor ) {
87- const el = document . querySelector ( to . hash )
91+ if ( isObject && shouldScroll . selector ) {
92+ const el = document . querySelector ( shouldScroll . selector )
8893 if ( el ) {
89- const docTop = document . documentElement . getBoundingClientRect ( ) . top
90- const elTop = el . getBoundingClientRect ( ) . top
91- position = {
92- x : window . scrollX ,
93- y : elTop - docTop
94- }
94+ position = getElementPosition ( el )
95+ } else if ( isValidPosition ( shouldScroll ) ) {
96+ position = normalizePosotion ( shouldScroll )
9597 }
98+ } else if ( isObject && isValidPosition ( shouldScroll ) ) {
99+ position = normalizePosotion ( shouldScroll )
96100 }
97101
98102 if ( position ) {
@@ -121,7 +125,7 @@ function pushState (url: string, replace?: boolean) {
121125 _key = genKey ( )
122126 history . pushState ( { key : _key } , '' , url )
123127 }
124- saveScrollPosition ( )
128+ saveScrollPosition ( _key )
125129 } catch ( e ) {
126130 window . location [ replace ? 'assign' : 'replace' ] ( url )
127131 }
@@ -130,16 +134,3 @@ function pushState (url: string, replace?: boolean) {
130134function replaceState ( url : string ) {
131135 pushState ( url , true )
132136}
133-
134- function saveScrollPosition ( ) {
135- if ( ! _key ) return
136- window . sessionStorage . setItem ( _key , JSON . stringify ( {
137- x : window . pageXOffset ,
138- y : window . pageYOffset
139- } ) )
140- }
141-
142- function getScrollPosition ( ) : ?{ x: number , y : number } {
143- if ( ! _key ) return
144- return JSON . parse ( window . sessionStorage . getItem ( _key ) )
145- }
0 commit comments