试图将滚动时的背景颜色更改为粘性标题,但不起作用。怎么做?当我滚动到正文底部时,我需要更改标题背景颜色。怎么做?
应用程序组件.html
<div class="header sticky">
</div>
应用组件.css
.sticky {
position: -webkit-sticky;
position: sticky;
top: 0px;
}
演示:https : //stackblitz.com/edit/sticky-header-hyibz9?file=app%2Fheader%2Fheader.component.css
您可以为此使用 ngClass 和 HostListener 来检查用户是否在按钮处滚动
html
<div class="header sticky" [ngClass]="isBottom ? 'bottom' : 'top'">
</div>
ts
import { Component, OnInit, HostListener } from '@angular/core';
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {
isBottom: boolean;
constructor() {}
ngOnInit() {}
@HostListener('window:scroll', [])
onScroll(): void {
if (window.innerHeight + window.scrollY >= document.body.offsetHeight) {
this.isBottom = true;
} else {
this.isBottom = false;
}
}
}
css
.sticky {
position: -webkit-sticky;
position: sticky;
top: 0px;
}
.header {
height: 50px;
}
.top {
background-color: blue;
}
.bottom {
background-color: yellow;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句