Files
angular-challenges/apps/performance/scroll-cd/src/app/app.component.ts
2023-10-18 14:08:15 +02:00

54 lines
1.1 KiB
TypeScript

import { AsyncPipe, NgIf } from '@angular/common';
import { Component, HostListener } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
@Component({
standalone: true,
imports: [NgIf, AsyncPipe],
selector: 'app-root',
template: `
<div>Top</div>
<div>Middle</div>
<div>Bottom</div>
<button (click)="goToTop()" *ngIf="displayButton$ | async">Top</button>
`,
styles: [
`
:host {
height: 1500px;
display: flex;
flex-direction: column;
justify-content: space-between;
button {
position: fixed;
bottom: 1rem;
left: 1rem;
z-index: 1;
padding: 1rem;
}
}
`,
],
})
export class AppComponent {
title = 'scroll-cd';
private displayButtonSubject = new BehaviorSubject<boolean>(false);
displayButton$ = this.displayButtonSubject.asObservable();
@HostListener('window:scroll', ['$event'])
onScroll() {
const pos = window.pageYOffset;
this.displayButtonSubject.next(pos > 50);
}
goToTop() {
window.scroll({
top: 0,
left: 0,
behavior: 'smooth',
});
}
}