2

I tried all the answers on this post in order to take the user to the top of the screen when they hit a link and change the route:

Angular 2 Scroll to top on Route Change

And they all work when the application is on a web browser, but I cannot make any of those different and right answers work on a mobile device, including the simplest:

window.scrollTo(0, 0);

I also tried viewportScroller

this.viewportScroller.scrollToPosition([0, 0]);

This does not work either:

imports: [
    RouterModule.forRoot(routes, {
      scrollPositionRestoration: 'top', 

    })

  ],

Does this something to do with the viewport meta, am I doing something wrong or should I change something there?

  <meta name="viewport" content="width=device-width, initial-scale=1">

Thanks.

Danielle
  • 1,386
  • 8
  • 22
  • https://stackoverflow.com/questions/39601026/angular-2-scroll-to-top-on-route-change?noredirect=1&lq=1 – Danish Dullu Nov 20 '21 at 19:09
  • Hi Danish. Yes, Ive seen that post and it all works fine on the browser, but not on mobile phone's browsers (at least for me). The accepted solution below worked perfect. – Danielle Nov 20 '21 at 19:16

1 Answers1

3

I had a similar issue before.

This worked for me

ngOnInit(): void {
    document.body.scrollTop = 0;
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
pvg1975
  • 273
  • 1
  • 10