本文介绍了Angular 8 @HostListener('window:scroll',[])不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图使用HostListener跟踪滚动位置以更改标题的颜色.
I have attempted to use HostListener to track the scroll position to change colour of the my header.
我的标头组件如下,
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',[])不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!