ChatGPT解决这个技术问题 Extra ChatGPT

Angular 5 在每次路线点击时滚动到顶部

我正在使用 Angular 5。我有一个仪表板,其中有几个部分的内容很小,而几个部分的内容很大,以至于在顶部时更改路由器时我遇到了问题。每次我需要滚动到顶部。

如何解决此问题,以便在更改路由器时,我的视图始终保持在顶部?


V
Vega

有一些解决方案,请确保全部检查:)

选项1:

每当实例化新组件时,路由器出口都会发出 activate 事件,因此我们可以使用 (activate) 滚动(例如)到顶部:

app.component.html

<router-outlet (activate)="onActivate($event)"></router-outlet>

app.component.ts

onActivate(event) {
   // window.scroll(0,0);

   window.scroll({ 
           top: 0, 
           left: 0, 
           behavior: 'smooth' 
    });

    //or document.body.scrollTop = 0;
    //or document.querySelector('body').scrollTo(0,0)
    ...
}

由于在 Safari 中没有很好地实现平滑滚动,因此使用 this solution 来实现平滑滚动:

onActivate(event) {
    let scrollToTop = window.setInterval(() => {
        let pos = window.pageYOffset;
        if (pos > 0) {
            window.scrollTo(0, pos - 20); // how far to scroll on each step
        } else {
            window.clearInterval(scrollToTop);
        }
    }, 16);
}

如果您希望有选择性,比如不是每个组件都应该触发滚动,您可以在 if 语句中检查它,如下所示:

onActivate(e) {
    if (e.constructor.name)==="login"{ // for example
            window.scroll(0,0);
    }
}

选项2:

从 Angular6.1 开始,我们还可以在急切加载的模块上使用 { scrollPositionRestoration: 'enabled' },它将应用于所有路由:

RouterModule.forRoot(appRoutes, { scrollPositionRestoration: 'enabled' })

它也将进行平滑滚动,已经。但是,这对于在每个路由上都执行此操作很不方便。

选项3:

另一种解决方案是在路由器动画上进行顶部滚动。在要滚动到顶部的每个过渡中添加此内容:

query(':enter, :leave', style({ position: 'fixed' }), { optional: true }) 

window 对象上的滚动事件在角度 5 中不起作用。任何猜测为什么?
@SahilBabbar,检查身体 css,溢出:隐藏?它的高度是多少?
@Vega 没有。 body 的高度是默认的,内部没有硬编码,因为它是一个普通的 Angular 5 应用程序。此外,请查看 Angular 文档,他们说 scroll 事件被 ngzones 列入黑名单。
尝试 document.body.scrollTop = 0;或使用旧的 js document.querySelector('body').scrollTo(0,0) 等。如果这些不起作用,请提供 MCVE
延迟加载模块有什么办法吗?
s
s.alem

如果您在 Angular 6 中遇到此问题,您可以通过将参数 scrollPositionRestoration: 'enabled' 添加到 app-routing.module.ts 的 RouterModule 来解决它:

@NgModule({
  imports: [RouterModule.forRoot(routes,{
    scrollPositionRestoration: 'enabled'
  })],
  exports: [RouterModule]
})

请注意,至少在 2019 年 11 月 6 日使用 Angular 8 时,scrollPositionRestoration 属性不适用于动态页面内容(即,页面内容是异步加载的):请参阅此 Angular 错误报告:github.com/angular/angular/issues/24547
G
GeoRover

编辑:对于 Angular 6+,请使用 Nimesh Nishara Indimagedara 的回答提到:

RouterModule.forRoot(routes, {
    scrollPositionRestoration: 'enabled'
});

原答案:

如果一切都失败了,那么在模板(或父模板)的顶部(或所需的滚动到位置)创建一些空的 HTML 元素(例如:div):

<div id="top"></div>

在组件中:

  ngAfterViewInit() {
    // Hack: Scrolls to top of Page after page view initialized
    let top = document.getElementById('top');
    if (top !== null) {
      top.scrollIntoView();
      top = null;
    }
  }

这个解决方案对我有用(在 Chrome 和 Edge 上测试)。接受的解决方案不适用于我的项目(Angular5)
@RobvanMeeuwen,如果我的回答不起作用,那么您可能没有以相同的方式实现它。该解决方案直接操作不正确的 DOM,既不安全
@Vega,这就是我称之为黑客的原因。您的解决方案是正确的。这里的一些人无法实现你的,所以我提供了后备黑客。他们应该根据他们目前所在的版本重构他们的代码。
所有这些解决方案都对我有用。谢谢@GeoRover
对于 Angular 6+,请使用 Nimesh Nishara Indimagedara 的答案。
T
Tim Diekmann

现在 Angular 6.1 中有一个内置的解决方案,带有 scrollPositionRestoration 选项。

请参阅 Angular 2 Scroll to top on Route Change 上的 my answer


V
Vikas

从 Angular 版本 6+ 开始不需要使用 window.scroll(0,0)

对于来自 @docs
的 Angular 版本 6+ 表示配置路由器的选项。

interface ExtraOptions {
  enableTracing?: boolean
  useHash?: boolean
  initialNavigation?: InitialNavigation
  errorHandler?: ErrorHandler
  preloadingStrategy?: any
  onSameUrlNavigation?: 'reload' | 'ignore'
  scrollPositionRestoration?: 'disabled' | 'enabled' | 'top'
  anchorScrolling?: 'disabled' | 'enabled'
  scrollOffset?: [number, number] | (() => [number, number])
  paramsInheritanceStrategy?: 'emptyOnly' | 'always'
  malformedUriErrorHandler?: (error: URIError, urlSerializer: UrlSerializer, url: string) => UrlTree
  urlUpdateStrategy?: 'deferred' | 'eager'
  relativeLinkResolution?: 'legacy' | 'corrected'
}

可以在中使用 scrollPositionRestoration?: 'disabled' | 'enabled' | 'top'

例子:

RouterModule.forRoot(routes, {
    scrollPositionRestoration: 'enabled'|'top' 
});

而如果需要手动控制滚动,则无需使用 window.scroll(0,0) 而是从 Angular V6 通用包中引入了 ViewPortScoller

abstract class ViewportScroller {
  static ngInjectableDef: defineInjectable({ providedIn: 'root', factory: () => new BrowserViewportScroller(inject(DOCUMENT), window) })
  abstract setOffset(offset: [number, number] | (() => [number, number])): void
  abstract getScrollPosition(): [number, number]
  abstract scrollToPosition(position: [number, number]): void
  abstract scrollToAnchor(anchor: string): void
  abstract setHistoryScrollRestoration(scrollRestoration: 'auto' | 'manual'): void
}

用法非常简单示例:

import { Router } from '@angular/router';
import {  ViewportScroller } from '@angular/common'; //import
export class RouteService {

  private applicationInitialRoutes: Routes;
  constructor(
    private router: Router;
    private viewPortScroller: ViewportScroller//inject
  )
  {
   this.router.events.pipe(
            filter(event => event instanceof NavigationEnd))
            .subscribe(() => this.viewPortScroller.scrollToPosition([0, 0]));
}

奇怪的是,每个解决方案在某些情况下都有效,而在其他情况下则失败。角度滚动到顶部存在严重缺陷。
我能够将 ViewportScroller 注入到我的组件中。它醒了。
S
Sal_Vader_808

尽管@Vega 直接回答了您的问题,但还是有问题。它破坏了浏览器的后退/前进按钮。如果您是用户单击浏览器的后退或前进按钮,他们会失去位置并在顶部滚动。如果您的用户不得不向下滚动以访问链接并决定单击返回却发现滚动条已重置到顶部,这对您的用户来说可能会有点痛苦。

这是我对问题的解决方案。

export class AppComponent implements OnInit {
  isPopState = false;

  constructor(private router: Router, private locStrat: LocationStrategy) { }

  ngOnInit(): void {
    this.locStrat.onPopState(() => {
      this.isPopState = true;
    });

    this.router.events.subscribe(event => {
      // Scroll to top if accessing a page, not via browser history stack
      if (event instanceof NavigationEnd && !this.isPopState) {
        window.scrollTo(0, 0);
        this.isPopState = false;
      }

      // Ensures that isPopState is reset
      if (event instanceof NavigationEnd) {
        this.isPopState = false;
      }
    });
  }
}

感谢您提供高级代码和不错的解决方案。但有时@Vega 解决方案更好,因为它解决了动画和动态页面高度的许多问题。如果您有包含内容和简单路由动画的长页面,则您的解决方案很好。我在带有许多动画和动态块的页面上尝试它,它看起来不太好。我认为有时我们可以为我们的应用牺牲“后退位置”。但如果不是 - 你的解决方案是我看到的最好的 Angular 解决方案。再次感谢你
Z
Zohab Ali

就我而言,我刚刚添加了

window.scroll(0,0);

ngOnInit() 中,它工作正常。


s
s sharif

Angular 6.1 及更高版本:

您可以使用 Angular 6.1+ 中提供的内置解决方案和选项 scrollPositionRestoration: 'enabled' 来实现相同的目的。

@NgModule({
  imports: [RouterModule.forRoot(routes,{
    scrollPositionRestoration: 'enabled'
  })],
  exports: [RouterModule]
})

Angular 6.0 及更早版本:

import { Component, OnInit } from '@angular/core';
import { Router, NavigationStart, NavigationEnd } from '@angular/router';
import { Location, PopStateEvent } from "@angular/common";

@Component({
    selector: 'my-app',
    template: '<ng-content></ng-content>',
})
export class MyAppComponent implements OnInit {

    private lastPoppedUrl: string;
    private yScrollStack: number[] = [];

    constructor(private router: Router, private location: Location) { }

    ngOnInit() {
        this.location.subscribe((ev:PopStateEvent) => {
            this.lastPoppedUrl = ev.url;
        });
        this.router.events.subscribe((ev:any) => {
            if (ev instanceof NavigationStart) {
                if (ev.url != this.lastPoppedUrl)
                    this.yScrollStack.push(window.scrollY);
            } else if (ev instanceof NavigationEnd) {
                if (ev.url == this.lastPoppedUrl) {
                    this.lastPoppedUrl = undefined;
                    window.scrollTo(0, this.yScrollStack.pop());
                } else
                    window.scrollTo(0, 0);
            }
        });
    }
}

注意:预期的行为是,当您导航回页面时,它应该保持向下滚动到单击链接时的相同位置,但在到达每个页面时滚动到顶部。


i
ivin antony

如果您使用 mat-sidenav 为路由器插座提供一个 ID(如果您有父路由器插座和子路由器插座)并在其中使用激活功能 <router-outlet id="main-content" (activate)="onActivate($event)"> 并使用此“mat-sidenav-content”查询选择器滚动顶部 {2 }


即使不使用 id 也能很好地工作(我的应用程序只有一个 router-outlet)。我也以更“角度”的方式做到了:@ViewChild(MatSidenavContainer) sidenavContainer: MatSidenavContainer; onActivate() { this.sidenavContainer.scrollable.scrollTo({ left: 0, top: 0 }); }
A
Akitha_MJ

对于一些正在寻找滚动功能的人,只需添加该功能并在需要时调用

scrollbarTop(){

  window.scroll(0,0);
}

J
Jared Whipple

我一直在寻找一个内置的解决方案来解决这个问题,就像在 AngularJS 中一样。但在那之前,这个解决方案对我有用,它很简单,并且保留了后退按钮的功能。

app.component.html

<router-outlet (deactivate)="onDeactivate()"></router-outlet>

app.component.ts

onDeactivate() {
  document.body.scrollTop = 0;
  // Alternatively, you can scroll to top by using this other call:
  // window.scrollTo(0, 0)
}

zurfyx original post 的回答


V
Vijay Barot

您只需要创建一个包含调整屏幕滚动的功能

例如

window.scroll(0,0) OR window.scrollTo() by passing appropriate parameter.

window.scrollTo(xpos, ypos) --> 预期参数。


R
Ria Pacheco

出于某种原因,以上方法都不适合我:/,因此我将元素引用添加到 app.component.html 中的顶部元素,并将 (activate)=onNavigate($event) 添加到 router-outlet

<!--app.component.html-->
<div #topScrollAnchor></div>
<app-navbar></app-navbar>
<router-outlet (activate)="onNavigate($event)"></router-outlet>

然后,我将子项添加到 app.component.ts 文件中的 ElementRef 类型,并在激活路由器插座时让它滚动到它。

export class AppComponent  {
  @ViewChild('topScrollAnchor') topScroll: ElementRef;

  onNavigate(event): any {
    this.topScroll.nativeElement.scrollIntoView({ behavior: 'smooth' });
  }
}

这是 stackblitz 中的代码


这也是唯一为我工作的人。我正在使用 ion-split-pane,window.scroll 似乎对它不起作用。
B
Breno Gomes

对我有用的解决方案:

document.getElementsByClassName('layout-content')[0].scrollTo(0, 0);

它适用于角度 8、9 和 10。


scrollTo(0, 0) 对我不起作用,但 scrollIntoView() 对我有用。不管怎么说,还是要谢谢你!
A
Anis KCHAOU

只需在 app.module.ts 文件中添加这一行:

RouterModule.forRoot(routes, {
    scrollPositionRestoration: 'enabled' //scroll to the top
})

我正在使用 Angular 11.1.4,它对我有用


T
Tsvetan Ganev

只需添加

window.scrollTo({ top: 0 });

ngOnInit()


window.scroll(0,0)
0
0_tr0jan_0

这是一个仅在第一次访问每个组件时才滚动到组件顶部的解决方案(以防您需要对每个组件执行不同的操作):

在每个组件中:

export class MyComponent implements OnInit {

firstLoad: boolean = true;

...

ngOnInit() {

  if(this.firstLoad) {
    window.scroll(0,0);
    this.firstLoad = false;
  }
  ...
}

E
Eray T

尝试这个:

app.component.ts

import {Component, OnInit, OnDestroy} from '@angular/core';
import {Router, NavigationEnd} from '@angular/router';
import {filter} from 'rxjs/operators';
import {Subscription} from 'rxjs';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss'],
})
export class AppComponent implements OnInit, OnDestroy {
    subscription: Subscription;

    constructor(private router: Router) {
    }

    ngOnInit() {
        this.subscription = this.router.events.pipe(
            filter(event => event instanceof NavigationEnd)
        ).subscribe(() => window.scrollTo(0, 0));
    }

    ngOnDestroy() {
        this.subscription.unsubscribe();
    }
}

i
irhetoric

导出类 AppComponent { 构造函数(私有路由器:路由器) { router.events.subscribe((val) => { if (val instanceof NavigationEnd) { window.scrollTo(0, 0); } }); } }


J
Joshua Michael Calafell

组件:订阅所有路由事件,而不是在模板中创建操作并在 NavigationEnd b/c 上滚动,否则您将在导航错误或路线阻塞等情况下触发此功能......这是一个肯定会知道的方法,如果成功导航到一条路线,然后安抚滚动。否则,什么也不做。

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit, OnDestroy {

  router$: Subscription;

  constructor(private router: Router) {}

  ngOnInit() {
    this.router$ = this.router.events.subscribe(next => this.onRouteUpdated(next));
  }

  ngOnDestroy() {
    if (this.router$ != null) {
      this.router$.unsubscribe();
    }
  }

  private onRouteUpdated(event: any): void {
    if (event instanceof NavigationEnd) {
      this.smoothScrollTop();
    }
  }

  private smoothScrollTop(): void {
    const scrollToTop = window.setInterval(() => {
      const pos: number = window.pageYOffset;
      if (pos > 0) {
          window.scrollTo(0, pos - 20); // how far to scroll on each step
      } else {
          window.clearInterval(scrollToTop);
      }
    }, 16);
  }

}

HTML

<router-outlet></router-outlet>

R
Rokive

尝试这个

@NgModule({
  imports: [RouterModule.forRoot(routes,{
    scrollPositionRestoration: 'top'
  })],
  exports: [RouterModule]
})

此代码支持角度 6<=


K
Kareem Adel

只需添加

 ngAfterViewInit() {
  window.scroll(0,0)
 }