@@ -126,13 +126,27 @@ export default function Viewport(elementRoutines, buffer, element, viewportContr
126
126
return ;
127
127
}
128
128
129
- // precise heights calculation, items that were in buffer once
130
- let topPaddingHeight = topPadding . cache . reduce ( ( summ , item ) => summ + ( item . index < buffer . first ? item . height : 0 ) , 0 ) ;
131
- let bottomPaddingHeight = bottomPadding . cache . reduce ( ( summ , item ) => summ + ( item . index >= buffer . next ? item . height : 0 ) , 0 ) ;
129
+ // precise heights calculation based on items that are in buffer or that were in buffer once
130
+ const visibleItemsHeight = buffer . reduce ( ( summ , item ) => summ + item . element . outerHeight ( true ) , 0 ) ;
131
+
132
+ let topPaddingHeight = 0 , topCount = 0 ;
133
+ topPadding . cache . forEach ( item => {
134
+ if ( item . index < buffer . first ) {
135
+ topPaddingHeight += item . height ;
136
+ topCount ++ ;
137
+ }
138
+ } ) ;
139
+
140
+ let bottomPaddingHeight = 0 , bottomCount = 0 ;
141
+ bottomPadding . cache . forEach ( item => {
142
+ if ( item . index >= buffer . next ) {
143
+ bottomPaddingHeight += item . height ;
144
+ bottomCount ++ ;
145
+ }
146
+ } ) ;
132
147
133
- // average item height based on buffer data
134
- let visibleItemsHeight = buffer . reduce ( ( summ , item ) => summ + item . element . outerHeight ( true ) , 0 ) ;
135
- let averageItemHeight = ( visibleItemsHeight + topPaddingHeight + bottomPaddingHeight ) / ( buffer . maxIndex - buffer . minIndex + 1 ) ;
148
+ const totalHeight = visibleItemsHeight + topPaddingHeight + bottomPaddingHeight ;
149
+ const averageItemHeight = totalHeight / ( topCount + bottomCount + buffer . length ) ;
136
150
137
151
// average heights calculation, items that have never been reached
138
152
let adjustTopPadding = buffer . minIndexUser !== null && buffer . minIndex > buffer . minIndexUser ;
0 commit comments