@@ -36,28 +36,16 @@ export function handleScroll (
36
36
37
37
// wait until re-render finishes before scrolling
38
38
router . app . $nextTick ( ( ) => {
39
- let position = getScrollPosition ( )
39
+ const position = getScrollPosition ( )
40
40
const shouldScroll = behavior ( to , from , isPop ? position : null )
41
+
41
42
if ( ! shouldScroll ) {
42
43
return
43
44
}
44
- const isObject = typeof shouldScroll === 'object'
45
- if ( isObject && typeof shouldScroll . selector === 'string' ) {
46
- const el = document . querySelector ( shouldScroll . selector )
47
- if ( el ) {
48
- let offset = shouldScroll . offset && typeof shouldScroll . offset === 'object' ? shouldScroll . offset : { }
49
- offset = normalizeOffset ( offset )
50
- position = getElementPosition ( el , offset )
51
- } else if ( isValidPosition ( shouldScroll ) ) {
52
- position = normalizePosition ( shouldScroll )
53
- }
54
- } else if ( isObject && isValidPosition ( shouldScroll ) ) {
55
- position = normalizePosition ( shouldScroll )
56
- }
57
45
58
- if ( position ) {
59
- window . scrollTo ( position . x , position . y )
60
- }
46
+ Promise . resolve ( shouldScroll ) . then ( ( shouldScroll ) => {
47
+ scrollToPosition ( shouldScroll , position )
48
+ } )
61
49
} )
62
50
}
63
51
@@ -109,3 +97,23 @@ function normalizeOffset (obj: Object): Object {
109
97
function isNumber ( v : any ) : boolean {
110
98
return typeof v === 'number'
111
99
}
100
+
101
+ function scrollToPosition ( shouldScroll , position ) {
102
+ const isObject = typeof shouldScroll === 'object'
103
+ if ( isObject && typeof shouldScroll . selector === 'string' ) {
104
+ const el = document . querySelector ( shouldScroll . selector )
105
+ if ( el ) {
106
+ let offset = shouldScroll . offset && typeof shouldScroll . offset === 'object' ? shouldScroll . offset : { }
107
+ offset = normalizeOffset ( offset )
108
+ position = getElementPosition ( el , offset )
109
+ } else if ( isValidPosition ( shouldScroll ) ) {
110
+ position = normalizePosition ( shouldScroll )
111
+ }
112
+ } else if ( isObject && isValidPosition ( shouldScroll ) ) {
113
+ position = normalizePosition ( shouldScroll )
114
+ }
115
+
116
+ if ( position ) {
117
+ window . scrollTo ( position . x , position . y )
118
+ }
119
+ }
0 commit comments