diff --git a/DynamicVirtualScroll.js b/DynamicVirtualScroll.js index bb5960e..91898f0 100644 --- a/DynamicVirtualScroll.js +++ b/DynamicVirtualScroll.js @@ -95,6 +95,10 @@ export function virtualScrollDriver(props, oldState, getRenderedItemHeight) firstVisibleItem = Math.floor(firstVisibleItem); let firstVisibleItemHeight = getRenderedItemHeight(firstVisibleItem) || newState.avgRowHeight; newState.topPlaceholderHeight = scrollTop - firstVisibleItemHeight*firstVisibleItemOffset; + if (newState.topPlaceholderHeight < 0) + { + newState.topPlaceholderHeight = 0; + } if (firstVisibleItem + newState.viewportItemCount >= props.totalItems - newState.viewportItemCount) { // Only one placeholder is required @@ -133,6 +137,10 @@ export function virtualScrollDriver(props, oldState, getRenderedItemHeight) sum += itemSize; } newState.middlePlaceholderHeight = newState.targetHeight - sum - newState.lastItemsTotalHeight - newState.topPlaceholderHeight; + if (newState.middlePlaceholderHeight < 0) + { + newState.middlePlaceholderHeight = 0; + } const correctedAvg = (sum + newState.lastItemsTotalHeight) / (newState.middleItemCount + newState.viewportItemCount); if (correctedAvg > newState.avgRowHeight) { diff --git a/DynamicVirtualScrollExample.js b/DynamicVirtualScrollExample.js index e3928f5..77e2075 100644 --- a/DynamicVirtualScrollExample.js +++ b/DynamicVirtualScrollExample.js @@ -33,7 +33,7 @@ export class DynamicVirtualScrollExample extends React.PureComponent // DOM example. As smooth as the previous one (memory example), even without caching if (this.itemElements[index]) { - return this.itemElements[index].offsetHeight; + return this.itemElements[index].getBoundingClientRect().height; } return 0; } diff --git a/README.md b/README.md index a57553a..04fcd38 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,9 @@ Description of parameters: * this function MUST return the height of currently rendered item or 0 if it's not currently rendered * the returned height MUST be >= props.minRowHeight * the function MAY cache heights of rendered items if you want your list to be more responsive + * WARNING: you SHOULD NOT use `element.offsetHeight` for measuring. Either use `element.getBoundingClientRect().height` + or use some pre-computed heights, because `offsetHeight` may truncate the height to -1px when + browser scale is not 100%. Also it gives incorrect results with CSS transforms. Returned object is `newState`. It contains the render parameters for you and also some internal state variables. What to do with it: