Angular 8 @HostListener('window:scroll', []) 不起作用 [英] Angular 8 @HostListener('window:scroll', []) not working

查看:37
本文介绍了Angular 8 @HostListener('window:scroll', []) 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 HostListener 跟踪滚动位置以更改标题的颜色.

I have attempted to use HostListener to track the scroll position to change colour of the my header.

我的header组件如下,

My header component is as follows,

import { Component, OnInit, Input, HostListener, Inject } from '@angular/core';
import { DOCUMENT } from '@angular/common';

@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.scss']
})
export class HeaderComponent implements OnInit {

constructor(@Inject(DOCUMENT) private document: Document) { }

  ngOnInit() {
  }

  @HostListener('window:scroll', [])
  onWindowScroll() {
    console.log(window.scrollY);
  }

}

但是当我滚动时,控制台中没有任何日志.

but when I am scrolling I am not getting any logs in console.

我尝试将 HostListener 放在主 app.component 中,因为我的标头组件正在定位,但是我仍然没有得到任何结果,也没有错误.

I have trying putting the HostListener in the main app.component, as my header component is positing fixed, However I am still getting no results, and no errors.

谢谢

推荐答案

这是因为styles.scss 中的全局样式

This is because of the global styles from styles.scss

从styles.scss改变高度到最小高度

From styles.scss change height to min-height

html, body {
    height: 100%;
}

为此

html, body {
    min-height: 100%;
}

这对我有用,希望能帮到你!

This is what worked for me, hope it help!

这篇关于Angular 8 @HostListener('window:scroll', []) 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆