mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-12 13:53:03 -05:00
feat(doc): move scroll-vd
This commit is contained in:
53
apps/performance/scroll-cd/src/app/app.component.ts
Normal file
53
apps/performance/scroll-cd/src/app/app.component.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
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',
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user