0

I am using Angular Material Virtual scroll, the items get loaded correctly into the DOM, but while scrolling it happens that it jumps around and automatically jumps to the end.

<cdk-virtual-scroll-viewport #scrollViewport  (scrolledIndexChange)="scrolled($event)" [itemSize]="ITEM_SIZE"  class="my-virtual" >
    <div *cdkVirtualFor="let elem of elemtArray;" class="input-field-container ">

        <div class="my-style" >{{elem}} </div>

    </div>
</cdk-virtual-scroll-viewport>

The output of the method scrolled is the following, if the glitch occurs:

Scrolled:  105
Scrolled:  115
Scrolled:  106
Scrolled:  117
Scrolled:  109
Scrolled:  119
Scrolled:  110
Scrolled:  121

If this happens, it automatically scrolls to the end of the virtual scroll.

Roy
  • 6,063
  • 3
  • 18
  • 43
Elrond
  • 329
  • 3
  • 16

1 Answers1

1

Virtual scroll relies on an calculatable element height to calculate the offsets. To control this, set the input itemSize of cdk-virtual-scroll-viewport to whatever height you expect your items to have (in px).

if your ITEM_SIZE does not match the actual item size, then your described behavior will happen.

example:

css:

.my-style {
    height: 42px;
}

html:

<cdk-virtual-scroll-viewport [itemSize]="42" [...]>
     <!-- ... --->
</cdk-virtual-scroll-viewport>
Mr.Manhattan
  • 4,795
  • 3
  • 18
  • 30
  • thanks for your answer, my Problem was that i used some special kind of styling inside my virtual scroll – Elrond May 12 '21 at 07:31