Skip to content

Commit 75b6ad5

Browse files
committed
fix(repeat): correct calculate distance to scroller for top buffer
1 parent fd45928 commit 75b6ad5

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/utilities-dom.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,27 @@ export const hasOverflowScroll = (element: HTMLElement): boolean => {
3535
let style = element.style;
3636
return style.overflowY === 'scroll' || style.overflow === 'scroll' || style.overflowY === 'auto' || style.overflow === 'auto';
3737
}
38+
39+
export const getDistanceToParent = (child: HTMLElement, parent: HTMLElement) => {
40+
const offsetParent = child.offsetParent as HTMLElement;
41+
const childOffsetTop = child.offsetTop;
42+
// [el] <-- offset parent === parent
43+
// [el] <-- child
44+
if (offsetParent === null || offsetParent === parent) {
45+
return childOffsetTop;
46+
}
47+
else {
48+
// [el] <-- offset parent
49+
// [el] <-- parent
50+
// [el] <-- child
51+
if (offsetParent.contains(parent)) {
52+
return childOffsetTop - parent.offsetTop;
53+
}
54+
// [el] <-- parent
55+
// [el] <-- offset parent
56+
// [el] <-- child
57+
else {
58+
return childOffsetTop + getDistanceToParent(offsetParent, parent);
59+
}
60+
}
61+
}

src/virtual-repeat.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ import {
3636
} from './utilities';
3737
import {
3838
getElementDistanceToTopOfDocument,
39-
hasOverflowScroll
39+
hasOverflowScroll,
40+
getDistanceToParent
4041
} from './utilities-dom';
4142
import { VirtualRepeatStrategyLocator } from './virtual-repeat-strategy-locator';
4243
import { TemplateStrategyLocator } from './template-strategy-locator';
@@ -565,7 +566,10 @@ export class VirtualRepeat extends AbstractRepeater {
565566
const topBuffer = this.topBufferEl;
566567
const scroller = this.scrollContainer;
567568
const itemHeight = this.itemHeight;
568-
const topBufferDistance = topBuffer.offsetTop - scroller.offsetTop;
569+
// If offset parent of top buffer is the scroll container
570+
// its actual offsetTop is just the offset top itself
571+
// If not, then the offset top is calculated based on the parent offsetTop as well
572+
const topBufferDistance = getDistanceToParent(topBuffer, scroller);
569573
const isFixedHeightContainer = this._fixedHeightContainer;
570574
/**
571575
* Real scroll top calculated based on current scroll top of scroller and top buffer {height + distance to top}

0 commit comments

Comments
 (0)