简体   繁体   中英

Angular - Hostlistener window:scroll function does not called after page reload

I have used following HostListener to sticky a menu top of page when scolling down.

sticky = false;

  @HostListener('window:scroll', ['$event'])
  handleScroll(): void {
    if (window.pageYOffset > 150) {
      this.sticky = true;
    } else {
      this.sticky = false;
    }
  }

It works well but after reload the page, The handleScroll() function is not callled

Call this method also in ngOnInit and then it is solved. Because when you reload page, the scroll event is not fired, because you do not scroll.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM