components icon indicating copy to clipboard operation
components copied to clipboard

[Tabs] Automatically page scrolls to top when switching between some tabs

Open hoangthau opened this issue 8 years ago • 53 comments

Bug, feature request, or proposal:

We found a bug. We had top content and a tab group below this content. And then we scrolled a little bit to bottom. We switched between some tabs. Now, page scrolled to top automatically.

It only occurs when we have top content on this page.

What is the expected behavior?

Page should keep scroll position when switching some tabs

What is the current behavior?

Page scrolls to top when switching some tabs

What are the steps to reproduce?

-Step 1: Scroll a little bit to bottom on page -Step 2: Click on Tab 3 The page will scroll to top.

Demo here: https://stackblitz.com/edit/angular-wjtrxy

What is the use-case or motivation for changing an existing behavior?

A bug

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Angular 5.0.3 - Material 5.0.2 - Window 10 - Typescript 2.4.2

Is there anything else we should know?

Thanks a lot!

hoangthau avatar Jan 25 '18 09:01 hoangthau

I have the same Issue. And I noticed that the height of the "top content" seems to have an influence on this problem. If you change the height to 900px for example, it doesn't scroll to top anymore.

DanielPinkpank avatar Feb 26 '18 09:02 DanielPinkpank

Confirmed. Have the same issue.

praveenpuglia avatar Feb 26 '18 09:02 praveenpuglia

👍 same issue

anshumansworld avatar Mar 05 '18 11:03 anshumansworld

same issue

umimehar avatar Mar 07 '18 10:03 umimehar

Well the reason is when you change the tab, the next tab not have that much height of its inner contents so the parent height also decrease. thats why the its scroll to top. try the parent element height fixed like 1000px and then change tab, it will not scroll up. 👍

umimehar avatar Mar 07 '18 10:03 umimehar

https://stackblitz.com/edit/angular-wjtrxy-g4ksm6

Add a min-height="800px" to parent elemnt of mat-tab-group. :)

umimehar avatar Mar 07 '18 10:03 umimehar

Thanks @umimehar. That's a good workaround.

hoangthau avatar Mar 08 '18 02:03 hoangthau

Seriously, why is there no clean solution to this existing problem starting from 2015!

TolgaBA avatar May 16 '18 09:05 TolgaBA

.fill-available {
  min-height: min-height: stretch;
}

With this class called in the main container of the page worked for me, only in chrome not in mozilla.

luchoman08 avatar Jun 13 '18 02:06 luchoman08

Better -webkit-fill-available (tested on google chrome). Here is an example https://stackblitz.com/edit/angular-wjtrxy-x9shma

luchoman08 avatar Jun 13 '18 02:06 luchoman08

None of the suggested solutions completely fix the problem for me, I still end up with some weird scrolling behaviors with a pixel min-height, stretch, fill-available...

edit: I modified @luchoman08 's stackblitz to reflect the problem: https://stackblitz.com/edit/angular-wjtrxy-utgzn1

I removed some content from tab1 so now if you go to tab4 for example, scroll down a bit, then click tab1, the scrolling bug is still there, switch between those 2 for extra sea sickness effects.

Lakston avatar Jun 28 '18 08:06 Lakston

still no solution for this? page shouldn't go up when click on the tab :/

imransilvake avatar Jul 14 '18 17:07 imransilvake

Still a problem with Angular 6.1 and Angular Material 6.4.

KlausHans avatar Aug 14 '18 11:08 KlausHans

min-height property don't work with MS Edge, still jumps to the top.

shifatul-i avatar Aug 25 '18 06:08 shifatul-i

Still a problem with angular 7.1.4 and material 7.2

RobChangCA avatar Dec 21 '18 14:12 RobChangCA

Same issue here.

aszorenyi avatar Feb 05 '19 13:02 aszorenyi

another hack approach, is to set min-height and padding/margin bottom for short content and to keep a footer in place.

min-height: 450px;
padding-bottom: 200px;
margin-bottom: -200px;

GSchutz avatar Feb 05 '19 20:02 GSchutz

"min-height" solution did not work for me. It works, but i had to set min-height to meaningless value. I wrote directive that works for me. You may have to change document.documentElement to your scrolling element. It is not extra clean solution, but it works.

import {MatTabChangeEvent, MatTabGroup} from '@angular/material';

@Directive({
  selector: '[mat-tab-scroll-fix]'
})
export class MatTabScrollFixDirective implements AfterViewInit {

  constructor(private matTabGroup: MatTabGroup) {

  }

  private scrollPosition: number;
  private tabChanging: boolean;

  ngAfterViewInit(): void {
    const scrollHandler = (event) => {
      if (this.tabChanging) {
        document.documentElement.scrollTop = this.scrollPosition;
      }
      this.scrollPosition = document.documentElement.scrollTop;
    };

    window.addEventListener('scroll', scrollHandler);

    this.matTabGroup.selectedTabChange.subscribe((tabChangeEvent: MatTabChangeEvent) => {
      this.tabChanging = false;
      document.documentElement.scrollTop = this.scrollPosition;
    });

    this.matTabGroup.selectedIndexChange.subscribe((index: number) => {
      this.tabChanging = true;
    });
  }
}

ludarous avatar Apr 09 '19 04:04 ludarous

Found same issue.

runningdavid avatar May 17 '19 18:05 runningdavid

Still have this problem. Angular 8

BobaFett58 avatar Jun 04 '19 08:06 BobaFett58