繁体   English   中英

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

[英]How to scroll with Router Navigation in Angular 7?

我的侧边栏导航组件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
        }
      }
    }
  }

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

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

Introduction.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'])
    }
  }

教育组成部分

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM