@@ -49,7 +49,7 @@ const VirtualList = Vue.component(NAME, {
49
49
uniqueIds : this . getUniqueIdFromDataSources ( )
50
50
} , this . onRangeChanged )
51
51
52
- // also need sync initial range first
52
+ // sync initial range
53
53
this . range = this . virtual . getRange ( )
54
54
55
55
// listen item size changing
@@ -87,8 +87,16 @@ const VirtualList = Vue.component(NAME, {
87
87
88
88
// set current scroll position to a expectant index
89
89
scrollToIndex ( index ) {
90
- const offset = this . virtual . getOffset ( index )
91
- this . scrollToOffset ( offset )
90
+ // scroll to top
91
+ if ( index === 0 ) {
92
+ this . scrollToOffset ( 0 )
93
+ } else if ( index >= this . dataSources . length - 1 ) {
94
+ // scroll to bottom
95
+ this . scrollToOffset ( this . getScrollSize ( ) )
96
+ } else {
97
+ const offset = this . virtual . getOffset ( index )
98
+ this . scrollToOffset ( offset )
99
+ }
92
100
} ,
93
101
94
102
// ----------- public method end -----------
@@ -97,6 +105,26 @@ const VirtualList = Vue.component(NAME, {
97
105
return this . dataSources . map ( ( dataSource ) => dataSource [ this . dataKey ] )
98
106
} ,
99
107
108
+ // get client viewport size (width or height)
109
+ getClientSize ( ) {
110
+ const { root } = this . $refs
111
+ if ( root ) {
112
+ return root [ this . isHorizontal ? 'clientWidth' : 'clientHeight' ]
113
+ } else {
114
+ return 0
115
+ }
116
+ } ,
117
+
118
+ // get all scroll size (width or height)
119
+ getScrollSize ( ) {
120
+ const { root } = this . $refs
121
+ if ( root ) {
122
+ return root [ this . isHorizontal ? 'scrollWidth' : 'scrollHeight' ]
123
+ } else {
124
+ return 0
125
+ }
126
+ } ,
127
+
100
128
// event called when each item mounted or size changed
101
129
onItemResized ( id , size ) {
102
130
this . virtual . saveSize ( id , size )
@@ -127,24 +155,24 @@ const VirtualList = Vue.component(NAME, {
127
155
}
128
156
129
157
const offset = root [ this . directionKey ]
130
- const offsetShape = root [ this . isHorizontal ? 'clientWidth' : 'clientHeight' ]
131
- const scrollShape = root [ this . isHorizontal ? 'scrollWidth' : 'scrollHeight' ]
158
+ const clientSize = this . getClientSize ( )
159
+ const scrollSize = this . getScrollSize ( )
132
160
133
161
// iOS scroll-spring-back behavior will make direction mistake
134
- if ( offset + offsetShape > scrollShape ) {
162
+ if ( offset + clientSize > scrollSize ) {
135
163
return
136
164
}
137
165
138
166
this . virtual . handleScroll ( offset )
139
- this . emitEvent ( offset , offsetShape , scrollShape , evt )
167
+ this . emitEvent ( offset , clientSize , scrollSize , evt )
140
168
} ,
141
169
142
170
// emit event in special position
143
- emitEvent ( offset , offsetShape , scrollShape , evt ) {
171
+ emitEvent ( offset , clientSize , scrollSize , evt ) {
144
172
const range = this . virtual . getRange ( )
145
173
if ( this . virtual . isFront ( ) && ! ! this . dataSources . length && offset - this . topThreshold <= 0 ) {
146
174
this . $emit ( 'totop' , evt , range )
147
- } else if ( this . virtual . isBehind ( ) && offset + offsetShape + this . bottomThreshold >= scrollShape ) {
175
+ } else if ( this . virtual . isBehind ( ) && offset + clientSize + this . bottomThreshold >= scrollSize ) {
148
176
this . $emit ( 'tobottom' , evt , range )
149
177
} else {
150
178
this . $emit ( 'scroll' , evt , range )
0 commit comments