[英]Material Angular scrolls to top on opening of mat-select and hides
In my application I'm using material select component. 在我的应用程序中,我正在使用材料选择组件。
Expected behavior I want to achieve is: when i open selector field that is bounded to selector should scroll to top of viewport and options should appear underneath. 我要实现的预期行为是:当我打开绑定到选择器的选择器字段时,应滚动到视口顶部,并且选项应显示在下方。
Demo : https://stackblitz.com/edit/angular-g6s73z 演示 : https : //stackblitz.com/edit/angular-g6s73z
I found an issue that when the selector is close to the bottom of viewport options will end up out of screen. 我发现一个问题,当选择器靠近视口底部时,选项将最终显示在屏幕外。
Issue : https://streamable.com/r3ekd 问题 : https : //streamable.com/r3ekd
I cannot find a way to fix it. 我找不到解决方法。 I tried implementing my custom scroll strategy where I was using
scrollIntoView
and then manually repositioned a select panel when it overflow a content. 我尝试在使用
scrollIntoView
地方实施自定义滚动策略,然后在内容溢出时手动重新scrollIntoView
选择面板。 It was buggy on android and iOS so i abandoned this idea. 它在Android和iOS上存在问题,因此我放弃了这个想法。
Did anyone had similar problem? 有人遇到过类似的问题吗? How to fix that issue?
如何解决这个问题? Maybe there's something I missed?
也许我错过了什么?
Custom scroll strategy 自定义滚动策略
enable() {
setTimeout(() => {
document.querySelector('#field').scrollIntoView({behavior: 'smooth'});
this.scrollDispatcher.scrolled().subscribe(() => {
this.overlayRef.updatePosition();
});
this.scrollDispatcher.scrolled().pipe(debounceTime(50), take(1)).subscribe(() => {
if (parseFloat(this.overlayRef.hostElement.style.top) <= 0) {
this.overlayRef.overlayElement.style.top = coerceCssPixelValue(12);
}
});
this.overlayRef.updatePosition();
disablePageScroll();
}
Nice observation. 不错的观察。
Upon investigation it became clear that 经调查,很明显
mat-select
was taken to the top of the view... and placement of the mat-select
was correct in all cases (when mat-select
was in the middle of the view and when mat-select
was at the very bottom of the view) mat-select
置于视图的顶部...并且在所有情况下, mat-select
都是正确的(当mat-select
位于视图的中间,而mat-select
位于视图的最底部时)视图) mat-select
it has the cdk-overlay-10
class on it; mat-select
时打开的div上带有cdk-overlay-10
类; On this div, There is a css bottom
value assigned to it inline [this is where the issue is and it is this bottom
value that pushes the div off-screen] bottom
值[这是问题所在,正是这个bottom
值将div推离了屏幕] top:0
on this div and our problem will be resolved... only one caveat that when we click on mat-select
2 things happen: (1) page scrolls (2) options div becomes visible... so an improved solution would be to have a custom animation that displays the div slowly so that the scrolling is finished before the div becomes visible. top:0
,我们的问题将得到解决...只有一个警告,当我们单击mat-select
会发生2件事:(1)页面滚动(2)选项div变得可见...因此,一种改进的解决方案是使自定义动画缓慢显示div,以便在div可见之前完成滚动。 relevant CSS : 相关CSS :
::ng-deep .cdk-overlay-pane{top:0 !important;
opacity: 1; animation:slowReveal 2s ease-in;
}
@keyframes slowReveal{
from{opacity:0.5;}
to{opacity:1;}
}
complete forked working stackblitz is here 完整的分叉工作stackblitz在这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.