尝试在ng2-bootstrap模式(整页)中创建scroll-then-fix div。我在这里看到两个问题: a)可以检测到'this.document.body.scrollTop'的值,但是无法检测到'this.el.nativeElement.offsetTop'。 b)sass类没有工作,因为div在bootstrap模式中。
HTML:在bootstrap模态体
中<div[class.fixed]="navIsFixed" #fixedButtons> </div>
打字稿:
@ViewChild ('fixedButtons') el: ElementRef;
@HostListener("window:scroll", [])
onWindowScroll() {
let number = this.el.nativeElement.offsetTop;
if (number > 65) {
this.navIsFixed = true;
} else if (this.navIsFixed && number < 10) {
this.navIsFixed = false;
}
}
SCSS:
.fixed {
position:fixed;
margin:0;
top:0;
}