我有一个使用角度4的应用程序。
滚动时我需要更改路线。
这是我的代码。
app.routing.module.ts
const routes: Routes = [
{ path: '', redirectTo: '/start', pathMatch: 'full' },
{ path: 'start', component: HomeComponent },
{ path: 'lazy', component: LazyComponent },
{ path: 'advanced', component: RestComponent },
{ path: 'basic', component: BasicComponent },
{ path: 'drilldown', component: DrilldownComponent },
{ path: 'styling', component: StylingComponent },
{ path: 'record-selection', component: RecordSelectionComponent },
{ path: 'totals', component: AggregateComponent },
{ path: 'custom-column', component: CustomColumnComponent },
{ path: 'column-click', component: ColumnClickComponent },
{ path: 'localization', component: LocalizationComponent },
{ path: 'inline-editing', component: InlineEditingComponent },
{ path: 'add-remove-edit', component: AddRemoveEditComponent },
{ path: 'column-settings-component', component: ChangeColumnSettingsComponent },
{ path: '**', component: HomeComponent }
];
我该怎么做?
答案 0 :(得分:0)
我不确定这是否是推荐的功能,但它是可能的。 我可以通过两种方式来解决这个问题。
@Directive({
selector: '[navigateOnScroll]'
})
export class NavigateOnScrollDirective implements OnDestroy {
didScroll: boolean;
lastScrollTop: number;
navbarHeight: number;
delta: number = 5; // set any value you wnat here
interval: any;
@HostListener('window:scroll', ['$event']) // this sets the listener to the scroll event
track() {
this.didScroll = true;
}
constructor(private router: Router, // import router for navigation purposes
private el: ElementRef) {
this.interval = setInterval(() => {
if (this.didScroll) {
this.hasScrolled();
this.didScroll = false;
}
}, 250);
}
hasScrolled() {
this.navbarHeight = this.el.nativeElement.offsetTop;
const st = window.pageYOffset;
// Make sure they scroll more than delta
if (Math.abs(this.lastScrollTop - st) <= this.delta) {
return;
}
// If they scrolled down and are past the navbar, add class
// This is necessary so you never see what is "behind" the navbar.
if (st > this.lastScrollTop && st > this.navbarHeight) {
// Scroll Down
this.router.navigate(['/your', 'path', 'here']);
} else {
// Scroll Up
this.router.navigate(['/your', 'path', 'here']);
}
this.lastScrollTop = st;
}
ngOnDestroy() {
clearInterval(this.interval);
}
}
*我在写这篇文章时跟踪了这篇文章,并为Angular 2+修改了它: https://medium.com/@mariusc23/hide-header-on-scroll-down-show-on-scroll-up-67bbaae9a78c
希望这有帮助,祝你好运!