本文介绍了如何在 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导航"><span class="navbar-toggler-icon"></span>按钮><div class="collapse navbar-collapse" id="navbarSupportedContent"><ul class="navbar-nav"><li class="nav-item"><a class="nav-link" routerLink="/about">关于</a><li class="nav-item"><a class="nav-link" routerLink="/experience">Experience</a><li class="nav-item"><a class="nav-link" routerLink="/education">教育</a>
</nav>
而 sidebar.component.ts
是这样的:
import { Component, OnInit } from '@angular/core';@成分({选择器:'应用侧边栏',templateUrl: './sidebar.component.html',styleUrls: ['./sidebar.component.css']})导出类 SidebarComponent 实现 OnInit {构造函数(){}ngOnInit() {}}
当我点击 About 或 Experience it 时,路由到那些组件,因为它们在我的 app-routing-module.ts
import { NgModule } from '@angular/core';从'@angular/router' 导入 { Routes, RouterModule };import { IntroductionComponent } from './introduction/introduction.component';从 './experience/experience.component' 导入 { ExperienceComponent };从 './education/education.component' 导入 { EducationComponent };const 路由:路由 = [{path: '', redirectTo: '/about', pathMatch: 'full'},{path: 'about', 组件: IntroductionComponent},{路径:'经验',组件:经验组件},{路径:'教育',组件:教育组件}];@NgModule({进口:[RouterModule.forRoot(routes)],出口:[路由器模块]})导出类 AppRoutingModule { }
我的app.component.html
是这样的.我通过它的选择器调用 sidebar.component app-sidebar
:
<app-sidebar></app-sidebar>
现在如何通过滚动来路由导航,这样当我向下滚动时,它会自动从体验"向下移动到教育",或者当我向上滚动时,它会自动从体验"到教育"移动到关于"部分.
解决方案
我的示例代码已上线
- /page1
- /page2
- /page3
并且在使用滚轮上下滚动时被激活,可以通过添加其他事件(例如使用向上和向下箭头键)来实现与我所做的相同的事情
在应用程序组件中,我为车轮移动设置了一个窗口侦听器,检查车轮是向上还是向下移动,最后但并非最不重要的是导航到想要的路线.
构造函数(专用路由器:路由器) { }@HostListener('window:wheel', ['$event'])onWheelScroll(事件:WheelEvent){//向下滚动如果 (evento.deltaY > 0) {开关(this.router.url){案例'/page1':{this.router.navigate(['/page2'])休息}案例'/page2':{this.router.navigate(['/page3'])休息}案例'/page3':{休息}}} else {//向上滚动开关(this.router.url){案例'/page1':{休息}案例'/page2':{this.router.navigate(['/page1'])休息}案例'/page3':{this.router.navigate(['/page2'])休息}}}}
这只是众多解决方案之一.
如果您希望仅当滚轮在组件内滚动时才发出事件,您也可以这样做:
introduction.component.ts
HostListener('wheel', ['$event'])onWheelScroll(事件:WheelEvent){//向上滚动如果 (evento.deltaY > 0) {this.router.navigate(['体验'])}}
experience.component.ts
HostListener('wheel', ['$event'])onWheelScroll(事件:WheelEvent){//向下滚动如果 (evento.deltaY > 0) {this.router.navigate(['教育'])} else {//向上滚动this.router.navigate(['about'])}}
education.component.ts
HostListener('wheel', ['$event'])onWheelScroll(事件:WheelEvent){//向上滚动如果(evento.deltaY
my sidebar navigation component sidebar.component.html
is like this:
<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>
and the sidebar.component.ts
is like this:
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() {
}
}
when I click on About or Experience it Routes to those components as they are mapped in my 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 { }
my app.component.html
is like this. I am calling the sidebar.component by its selector app-sidebar
:
<router-outlet></router-outlet>
<app-sidebar></app-sidebar>
Now how to route the navigation by scrolling such that when I scroll down it automatically moves down from about to experience to education or when I scroll up it automatically moves up from experience to education to about sections.
解决方案
My example code is on
- /page1
- /page2
- /page3
and is activated when the wheel is used to scroll down and up, it's possible to implement the same thing that I did by adding other events (like with up and down arrow key)
In the app component, I set a window listener for wheel movement, check if the wheel is moving up or down, and last but not least navigate to the wanted route.
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
}
}
}
}
This is just one of many solutions.
If you want that the event is emitted only if the wheel is scrolled inside the component you can do like this too:
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'])
}
}
education.component.ts
HostListener('wheel', ['$event'])
onWheelScroll(evento: WheelEvent) {
// Scroll up
if (evento.deltaY < 0) {
this.router.navigate(['experience'])
}
}
这篇关于如何在 Angular 7 中使用路由器导航滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!