In my Angular App (currently Angular 11) I always used a back-to-top-button which appears when the user scrolls. The button scrolls the window back to top when it gets clicked and then disappears. Classic behaviour.
But now I changed my layout and replaced bootstrap navbars 'n stuff by Angular Material Tabs.
My BodyComponent now looks somehow like this:
<div id="body.component.container" style="margin-top: 62px;">
<mat-tab-group [(selectedIndex)]="selectedMatTabIndex">
<mat-tab>
<ng-template matTabLabel>
<span style="font-family: 'Arial Narrow', monospace; font-size: 16px; font-weight: bold;">Tab1</span>
</ng-template>
<app-content-component-001></app-content-component-001>
</mat-tab>
<mat-tab>
<ng-template matTabLabel>
<span style="font-family: 'Arial Narrow', monospace; font-size: 16px; font-weight: bold;">Tab2</span>
</ng-template>
<app-content-component-002></app-content-component-002>
</mat-tab>
</mat-tab-group>
</div>
<app-back-to-top
[acceleration]="1000"
[animate]="true"
[scrollDistance]="50"
[speed]="5000">
</app-back-to-top>
The problem I am facing is, that there is no common scrolling event catchable any more. Usually, As you all surely know, inside a back-to-top-button component one listens to the HostEvent window:scroll
but this does not work inside MatTabs.
@HostListener('window:scroll', [])
onWindowScroll() {
if (this.isBrowser()) {
this.animationState = this.getCurrentScrollTop() > this.scrollDistance / 2 ? 'in' : 'out';
}
}
And it does not matter where I put this back-to-top-button-component at. I tried putting it directly into the body component (that's how it worked out for years), into the container-div, into the MatTabGroup and into each MatTab. The window:scroll
-event does not show up.
During my (Re)Searching through the internet I found some faint hints that I have to use some directives of CDK's but no example how.
So I've got to questions.
- How to detect the scoll event inside Material Tabs in order to get my back-to-top-button fading in again?
- How to Scroll back to top inside Material Tabs programmatically?