简体   繁体   English

材质角在滚动选择垫并隐藏时滚动到顶部

[英]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位于视图的最底部时)视图)
  • the issue was with the div which opens up when we click on mat-select it has the cdk-overlay-10 class on it; 问题在于div,当我们单击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] 在此div上,向其内联分配了一个CSS bottom值[这是问题所在,正是这个bottom值将div推离了屏幕]
  • solution is: set a 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. 解决方案是:在此div上设置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.

相关问题 如何在 mat-select Angular Material 上显示预览值 - How to display a preview value on mat-select Angular Material 角材料垫选择设置默认选择值 - Angular Material mat-select set default selected value 无法设置设置 Angular 材质垫-选择默认选项 - Cannot set set Angular Material mat-select default option 赛普拉斯 e2e 测试与 Angular 材料垫选择不能设置 select 值 - Cypress e2e test with Angular Material mat-select can't set a select value 如何将Angular 5 Material Select下拉菜单(mat-select)更改为打字稿代码/文件中的选项之一? - How can I change an Angular 5 Material Select dropdown (mat-select) to one of the options in typescript code/file? 如何使用 *ngFor 获取 Angular Material mat-select 中的值和文本? - How to get the value and the text in an Angular Material mat-select by using *ngFor? 垫选择组件的角补丁值 - Angular patchValue of a mat-select component Angular mat-select 不能正确地使用多个样式 - Angular mat-select not styling correctly with multiple 关闭垫选择后如何滚动垫选择选项顶部? - How to scroll mat-select option top after closing the mat-select? 在多个 select 的情况下隐藏 angular mat-select 的复选框? - Hide checkbox from angular mat-select in case of multiple select?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM