import { ChangeDetectionStrategy, Component, ElementRef, viewChild, } from '@angular/core' import { injectWindowVirtualizer } from '@tanstack/angular-virtual' @Component({ selector: 'app-root', standalone: true, changeDetection: ChangeDetectionStrategy.OnPush, template: `

In many cases, when implementing a virtualizer with a window as the scrolling element, developers often find the need to specify a "scrollMargin." The scroll margin is a crucial setting that defines the space or gap between the start of the page and the edges of the list.

Window Scroller

@for (row of virtualizer.getVirtualItems(); track row.key) {
Row {{ row.index }}
}
`, styles: ` .scroll-container { height: 400px; width: 400px; overflow-y: auto; contain: 'strict'; } `, }) export class AppComponent { scrollElement = viewChild>('scrollElement') virtualizer = injectWindowVirtualizer(() => ({ count: 10000, estimateSize: () => 35, overscan: 5, scrollMargin: this.scrollElement()?.nativeElement.offsetTop, })) }