template - <div #someDiv></div> <!--про высоту не забывай-->
component -
private destroy$ = new Subject();
@ViewChild('someDiv', {read: ElementRef}) set content(content: ElementRef) {
if (content) {
fromEvent(content.nativeElement, 'scroll')
.pipe(
takeUntil(this.destroy$),
)
.subscribe((event) => {
const bounding = event.target.getBoundingClientRect();
const absoluteElementTop =
bounding.top + window.pageYOffset;
const middle = absoluteElementTop - (bounding.height / 2);
window.scrollTo(0, middle);
});
}
}
public ngOnDestroy(): void {
this.destroy$.next(true);
this.destroy$.complete();
}