mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-11 13:23:02 -05:00
54 lines
1.1 KiB
TypeScript
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',
|
|
});
|
|
}
|
|
}
|