Skip to content

Instantly share code, notes, and snippets.

@eneajaho
Created November 25, 2021 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eneajaho/2caf4c0f3336116b4d50d8447419b702 to your computer and use it in GitHub Desktop.
Save eneajaho/2caf4c0f3336116b4d50d8447419b702 to your computer and use it in GitHub Desktop.
Scroll to top - Angular directive
import { Directive, HostListener, Inject, NgModule } from '@angular/core';
import { WINDOW } from "@ng-web-apis/common";
@Directive({ selector: '[scrollTop]' })
export class ScrollTopDirective {
constructor(@Inject(WINDOW) private window: Window) {}
@HostListener('click', ['$event'])
handleClick(event: PointerEvent): void {
this.window.scrollTo(0, 0);
}
}
@eneajaho
Copy link
Author

Usage:

Component:

<button scrollTop type="button">Scroll top</button>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment