I’m working on an Angular project. I implemented an “infinite scroll” list utilizing IntersectionObvserver. Generally it works quite well and satisfying.
But there is one issue I couldn’t find a solution for so far.
The results to build the list are being sorted ascending.
In Angular this causes the problem that the HTML list is built upwards while the scrollbar remains at the same position that triggered the infinite scroll event, which causes the event to fire over and over and over again until the user scrolls away manually.
That the event is being fired as long as the infinite scroll trigger is in the viewport is desired behavior, because depending on the viewport height and user preferences it can’t be guaranteed that there will always be enough items to fill the whole page initially. New items must be loaded continously until the page is filled.
StackBlitz example
https://stackblitz.com/edit/angular-infinite-scroll-intersectionobserver
Scroll to the very end of the list to trigger the infinite scroll event. It will keep the scrollbar at the same position and firing the event while building the list upwards.
In the generateItems()
function there is a call to sortItems()
. When you disable this call the scrolling behavior of the infinite scroll list works properly. New items added to the list cause the list to grow and push the infinite scroll trigger out of the viewport.
CodePen example
When I implement the same infinite scroll logic using native JavaScript
with jQuery
it works as expected, even when the results are sorted.
https://codepen.io/mw-108/pen/ExqPKmG
Here the infinite scroll trigger is always pushed out of the viewport when the list is updated. This is what I expect.
Questions
Am I doing something wrong in Angular?
What can I do to reproduce the same result I see in the JavaScript / jQuery example in Angular?