We are trying to scroll to a specific <mat-expansion-panel> item within a <mat-accordion>. The problem is that ngAfterViewInit() is triggered before the accordion and its panels are fully loaded. This means the scrollIntoView() function is called while the accordions are being loaded and the page size afterwards change making our scroll operation take us to the wrong position of the page.
We also tried the other lifecycle hooks, which did not help, since they're all called to early. Does somebody have any good practice for this issue?
Our source is simple since we are trying to implement something very basic:
landingpage.component.html
<mat-accordion>
<mat-expansion-panel id="pangel-1">
<mat-expansion-panel-header>
<mat-panel-title>Lorem ipsum</mat-panel-title>
</mat-expansion-panel-header>
<p>
Lorem ipsum dolor sit amet,
consetetur sadipscing elitr,
sed diam nonumy eirmod tempor invidunt...
</p>
</mat-expansion-panel>
<mat-expansion-panel id="panel-2">
<mat-expansion-panel-header>
<mat-panel-title>Lorem ipsum</mat-panel-title>
</mat-expansion-panel-header>
<p>
Lorem ipsum dolor sit amet,
consetetur sadipscing elitr,
sed diam nonumy eirmod tempor invidunt...
</p>
</mat-expansion-panel>
[ ... ] // more panels
</mat-accordion>
landingpage.component.ts
ngAfterViewInit() {
this.scroll("panel-1");
}
scroll(id) {
console.log(`scrolling to ${id}`);
let el = document.getElementById(id);
el.scrollIntoView();
}
from Scroll to specific Expansion Panel in Material Accordion after Component loaded
No comments:
Post a Comment