1 回答
TA贡献1776条经验 获得超12个赞
做了一个新的导航服务:
public active_fragment: BehaviorSubject<string> = new BehaviorSubject('');
constructor(private readonly route: ActivatedRoute) {
this.route.fragment.subscribe((frag) => {
this.active_fragment.next(frag);
});
}
在我的导航组件中:
[ngClass]="{
'nav-active': (navigationService.active_fragment | async) === 'home'
}"
在包含我的片段的登陆组件中:
@ViewChild('features', { read: ElementRef })
private _features: ElementRef;
@ViewChild('benefits', { read: ElementRef })
private _benefits: ElementRef;
@HostListener('window:scroll', ['$event'])
private _update_active_fragment(event: any) {
event.preventDefault();
switch (true) {
case window.pageYOffset >=
this._some_previous_element.nativeElement.offsetTop &&
window.pageYOffset <= this._features.nativeElement.offsetTop: {
this.navigationService.active_fragment.next('network-rail-feedback');
break;
}
case window.pageYOffset >= this._features.nativeElement.offsetTop &&
window.pageYOffset <= this._benefits.nativeElement.offsetTop: {
this.navigationService.active_fragment.next('features');
break;
}
default:
break;
}
}
添加回答
举报