Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LitElement infinite scroll reposition issue

I'm creating a POC for a LitElement infinite scroller that limits the DOM count of the list. The component is based on this one: https://medium.com/walmartglobaltech/infinite-scrolling-the-right-way-11b098a08815

My POC is here: https://stackblitz.com/edit/lit-element-infinite-scroller-poc

The item positioning is achieved by adjusting the paddings and updating the list item content when the IntersectionObserver is triggered.

When the component is scrolled really fast from top to bottom, it works fine. When you slow down the scroll, it sometimes adds a padding before reaching the threshold of the IntersectionObserver. As for scrolling from bottom to top, it always behaves like that regardless of whether you scroll fast or slowly.

I'm guessing it's a timing issue but I can't pinpoint where exactly.

like image 961
dork Avatar asked Feb 28 '26 06:02

dork


2 Answers

I am not sure if it is a good idea to reinvent the wheel and implement infinite scroll by yourself, but for your code the issue seems to be in _getNewTopItemIndex method specifically when isScrollDown is false you seems to be having typo there as that part is not same as medium post you mentioned in your question.

_getNewTopItemIndex(isScrollDown) {
  const increment = this.listSize / 2;
  const newIndex = isScrollDown
    ? this._top.index + increment
    : this._top.index - increment;  // original code: this._top.index - increment - this.listSize;

  return newIndex < 0 ? 0 : newIndex;
}
like image 138
Dipen Shah Avatar answered Mar 02 '26 20:03

Dipen Shah


As Dipen says, I feel like _getNewTopItemIndex can be optimized but really if you see lit-visualizer it's quite laborious to do this implementation.

I recommend you not to reinvent the wheel, you can use lit-virtualizer

https://www.npmjs.com/package/lit-virtualizer

try with

npm i lit-virtualizer

I think you need something else you can collaborate with the project (https://github.com/polymerlabs/uni-virtualizer)

Example:

render() {
        return html`
            <lit-virtualizer
              .scrollTarget=${window}
              .items=${this.data}
              .renderItem=${(contact) => html`
                <div><b>${contact.name}</b>: ${contact.phone}</div>
              `}>
            </lit-virtualizer>
        `;
    }
like image 23
HerberthObregon Avatar answered Mar 02 '26 21:03

HerberthObregon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!