Skip to content

Commit

Permalink
fix(virtual-repeat): properly calc for fixheight ct
Browse files Browse the repository at this point in the history
  • Loading branch information
bigopon committed Mar 5, 2019
1 parent e8e14a1 commit 5171740
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/template-strategy-default.ts
Expand Up @@ -43,6 +43,6 @@ export class DefaultTemplateStrategy implements ITemplateStrategy {
} }


getTopBufferDistance(topBuffer: HTMLElement): number { getTopBufferDistance(topBuffer: HTMLElement): number {
return topBuffer.offsetTop; return 0;
} }
} }
1 change: 1 addition & 0 deletions src/utilities.ts
Expand Up @@ -71,6 +71,7 @@ export function getElementDistanceToTopViewPort(element: Element): number {
return element.getBoundingClientRect().top; return element.getBoundingClientRect().top;
} }


export const Math$abs = Math.abs;
export const Math$max = Math.max; export const Math$max = Math.max;
export const Math$min = Math.min; export const Math$min = Math.min;
export const Math$round = Math.round; export const Math$round = Math.round;
Expand Down
16 changes: 10 additions & 6 deletions src/virtual-repeat.ts
Expand Up @@ -31,7 +31,8 @@ import {
getStyleValues, getStyleValues,
Math$ceil, Math$ceil,
Math$floor, Math$floor,
Math$max Math$max,
Math$abs
} from './utilities'; } from './utilities';
import { import {
getElementDistanceToTopOfDocument, getElementDistanceToTopOfDocument,
Expand Down Expand Up @@ -561,8 +562,11 @@ export class VirtualRepeat extends AbstractRepeater {
if (!items) { if (!items) {
return; return;
} }
const topBuffer = this.topBufferEl;
const scroller = this.scrollContainer;
const itemHeight = this.itemHeight; const itemHeight = this.itemHeight;
const topBufferDistance = this.templateStrategy.getTopBufferDistance(this.topBufferEl); const topBufferDistance = topBuffer.offsetTop - scroller.offsetTop;
const isFixedHeightContainer = this._fixedHeightContainer;
/** /**
* Real scroll top calculated based on current scroll top of scroller and top buffer {height + distance to top} * Real scroll top calculated based on current scroll top of scroller and top buffer {height + distance to top}
* as there could be elements before top buffer and it affects real scroll top of the selected repeat * as there could be elements before top buffer and it affects real scroll top of the selected repeat
Expand All @@ -571,15 +575,15 @@ export class VirtualRepeat extends AbstractRepeater {
* - not document: the scroll top of this repeat is calculated based on current scroller.scrollTop and the distance to top of `top buffer` * - not document: the scroll top of this repeat is calculated based on current scroller.scrollTop and the distance to top of `top buffer`
* - document: the scroll top is the substraction of `pageYOffset` and distance to top of current buffer element (logic needs revised) * - document: the scroll top is the substraction of `pageYOffset` and distance to top of current buffer element (logic needs revised)
*/ */
const scrollTop = this._fixedHeightContainer const scrollTop = isFixedHeightContainer
? (this.scrollContainer.scrollTop - topBufferDistance) ? scroller.scrollTop
: (pageYOffset - this.distanceToTop); : (pageYOffset - this.distanceToTop);
const scrollerInfo = this.getScrollerInfo(); const realScrollTop = Math$max(0, isFixedHeightContainer ? scrollTop - Math$abs(topBufferDistance): scrollTop)
const elementsInView = this.elementsInView; const elementsInView = this.elementsInView;


// Calculate the index of first view // Calculate the index of first view
// Using Math floor to ensure it has correct space for both small and large calculation // Using Math floor to ensure it has correct space for both small and large calculation
const firstIndex = Math$max(0, itemHeight > 0 ? Math$floor(scrollTop / itemHeight) : 0); const firstIndex = Math$max(0, itemHeight > 0 ? Math$floor(realScrollTop / itemHeight) : 0);
this._first = firstIndex; this._first = firstIndex;
// if first index starts somewhere after the last view // if first index starts somewhere after the last view
// then readjust based on the delta // then readjust based on the delta
Expand Down

0 comments on commit 5171740

Please sign in to comment.