[Tabs] Automatically page scrolls to top when switching between some tabs
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!
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.
Confirmed. Have the same issue.
👍 same issue
same issue
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. 👍
https://stackblitz.com/edit/angular-wjtrxy-g4ksm6
Add a min-height="800px" to parent elemnt of mat-tab-group. :)
Thanks @umimehar. That's a good workaround.
Seriously, why is there no clean solution to this existing problem starting from 2015!
.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.
Better -webkit-fill-available (tested on google chrome). Here is an example https://stackblitz.com/edit/angular-wjtrxy-x9shma
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.
still no solution for this? page shouldn't go up when click on the tab :/
Still a problem with Angular 6.1 and Angular Material 6.4.
min-height property don't work with MS Edge, still jumps to the top.
Still a problem with angular 7.1.4 and material 7.2
Same issue here.
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;
"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;
});
}
}
Found same issue.
Still have this problem. Angular 8