Home > Back-end >  Changing route not scrolling to the top of the screen on mobile
Changing route not scrolling to the top of the screen on mobile

Time:11-21

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.

CodePudding user response:

I had a similar issue before.

This worked for me

ngOnInit(): void {
    document.body.scrollTop = 0;
}
  • Related