Skip to content

Commit e0be2e6

Browse files
committed
average item height calculation refactoring
1 parent 9fb9ffc commit e0be2e6

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/modules/viewport.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,27 @@ export default function Viewport(elementRoutines, buffer, element, viewportContr
126126
return;
127127
}
128128

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+
});
132147

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);
136150

137151
// average heights calculation, items that have never been reached
138152
let adjustTopPadding = buffer.minIndexUser !== null && buffer.minIndex > buffer.minIndexUser;

0 commit comments

Comments
 (0)