我是angular4的新手,我正在寻找解释滚动的任何教程。正如标题所述,我想在第二个特定位置
时更改标题的css属性非常感谢任何帮助。我根本不知道从哪里开始
答案 0 :(得分:11)
import { Component, OnInit } from '@angular/core';
import { PageEvent } from '@angular/material';
import { HostListener, Inject } from "@angular/core";
import { DOCUMENT } from '@angular/platform-browser';
declare const window: any;
@Component({
selector: 'app-client-product-prev',
templateUrl: './client-product-prev.component.html',
styleUrls: ['./client-product-prev.component.css'],
})
export class IndexComponent implements OnInit {
constructor(){
}
ngOnInit() {}
// ===========================================================================
// TRY THIS
// ===========================================================================
@HostListener("window:scroll", [])
onWindowScroll() {
const number = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
if (number > 100) {
console.log('You are 100px from the top to bottom');
} else if (number > 500) {
console.log('You are 500px from the top to bottom');
}
}
}
答案 1 :(得分:1)
只需权衡一下,因为今天我有类似的愿望,即在滚动时缩小/放大标题栏中的文本。很简单。
在我的header.component.ts中:
从'@ angular / core'导入{Component,HostListener,OnInit};
然后
@HostListener('window:scroll', [])
onWindowScroll() {
const scrollOffset = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
if (scrollOffset >= 120) {
document.querySelectorAll('.controllable').forEach((c) => {
c.classList.add('text-smaller');
c.classList.remove('text-larger');
});
} else {
document.querySelectorAll('.controllable').forEach((c) => {
c.classList.add('text-larger');
c.classList.remove('text-smaller');
});
}
}
在HTML中:
<span class="menu-link controllable">{{ link.label }}</span>
和CSS:
.menu-link.controllable.text-larger {
font-size: 18px;
}
.menu-link.controllable.text-smaller {
font-size: 14px;
}