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

编程入门 行业动态 更新时间:2024-10-27 12:40:36
本文介绍了Angular 8 @HostListener('window:scroll', []) 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我尝试使用 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: './headerponent.html',
  styleUrls: ['./headerponent.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 放在主 appponent 中,因为我的标头组件正在定位,但是我仍然没有得到任何结果,也没有错误.

I have trying putting the HostListener in the main appponent, 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-22 11:42:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1023498.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   HostListener   Angular   scroll   window

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!