gpt4 book ai didi

angular - 如何在 Angular 7 中使用路由器导航进行滚动?

转载 作者:行者123 更新时间:2023-12-02 17:57:10 25 4
gpt4 key购买 nike

我的侧边栏导航组件sidebar.component.html是这样的:

<nav class="navbar navbar-expand-lg navbar-dark bg-primary fixed-top" id="sideNav">
<a class="navbar-brand" href="#page-top">
<span class="d-block d-lg-none">Sujoy Debnath</span>
<span class="d-none d-lg-block">
<img class="img-fluid img-profile rounded-circle mx-auto mb-2" src="assets/img/resume.jpg" alt="">
</span>
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data- target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" routerLink="/about">About</a>
</li>
<li class="nav-item">
<a class="nav-link" routerLink="/experience">Experience</a>
</li>
<li class="nav-item">
<a class="nav-link" routerLink="/education">Education</a>
</li>
</ul>
</div>
</nav>
sidebar.component.ts 是这样的:

import { Component, OnInit } from '@angular/core';

@Component({
selector: 'app-sidebar',
templateUrl: './sidebar.component.html',
styleUrls: ['./sidebar.component.css']
})
export class SidebarComponent implements OnInit {

constructor() { }

ngOnInit() {
}

}

当我单击“关于”或“体验”时,它会路由到这些组件,因为它们映射到我的 app-routing-module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { IntroductionComponent } from './introduction/introduction.component';
import { ExperienceComponent } from './experience/experience.component';
import { EducationComponent } from './education/education.component';

const routes: Routes = [
{path: '', redirectTo: '/about', pathMatch: 'full'},
{path: 'about', component: IntroductionComponent},
{path: 'experience', component: ExperienceComponent},
{path: 'education', component: EducationComponent}

];

@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }

我的app.component.html是这样的。我通过选择器 app-sidebar 调用 sidebar.component:

<router-outlet></router-outlet>
<app-sidebar></app-sidebar>

现在如何通过滚动来路由导航,以便当我向下滚动时,它会自动从“关于”向下移动到“体验”到“教育”,或者当我向上滚动时,它会自动从“体验”移动到“教育”到“关于”部分。

最佳答案

我的示例代码已打开

  1. /第1页
  2. /第2页
  3. /第3页

并且在使用滚轮向下和向上滚动时被激活,可以通过添加其他事件(例如使用向上和向下箭头键)来实现与我所做的相同的事情

在应用程序组件中,我设置了一个用于轮子移动的窗口监听器,检查轮子是否向上或向下移动,最后但并非最不重要的导航到所需的路线。

  constructor(
private router: Router
) { }

@HostListener('window:wheel', ['$event'])
onWheelScroll(evento: WheelEvent) {
// Scroll down
if (evento.deltaY > 0) {
switch (this.router.url) {
case '/page1': {
this.router.navigate(['/page2'])
break
}
case '/page2': {
this.router.navigate(['/page3'])
break
}
case '/page3': {
break
}
}
} else { // Scroll up
switch (this.router.url) {
case '/page1': {
break
}
case '/page2': {
this.router.navigate(['/page1'])
break
}
case '/page3': {
this.router.navigate(['/page2'])
break
}
}
}
}

这只是众多解决方案之一。

如果您希望仅当滚轮在组件内滚动时才发出事件,您也可以这样做:

介绍.component.ts

  HostListener('wheel', ['$event'])
onWheelScroll(evento: WheelEvent) {
// Scroll up
if (evento.deltaY > 0) {
this.router.navigate(['experience'])
}
}

experience.component.ts

  HostListener('wheel', ['$event'])
onWheelScroll(evento: WheelEvent) {
// Scroll down
if (evento.deltaY > 0) {
this.router.navigate(['education'])
} else { // Scroll up
this.router.navigate(['about'])
}
}

education.component.ts

  HostListener('wheel', ['$event'])
onWheelScroll(evento: WheelEvent) {
// Scroll up
if (evento.deltaY < 0) {
this.router.navigate(['experience'])
}
}

关于angular - 如何在 Angular 7 中使用路由器导航进行滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53998080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com