简体   繁体   English

Angular Material - Mat-Dialog - 改变背景模糊/变暗效果

[英]Angular Material - Mat-Dialog - change background blur / darkening effect

Hello my beloved community,你好,我亲爱的社区,

Using angular with angular material.使用带角材料的角。

With the default configuration when you open up a material dialog, it darkens the background a bit.当您打开材质对话框时,使用默认配置,它会使背景变暗一点。 Now I would like it to be a blurred background instead.现在我希望它是一个模糊的背景。 I tried playing around with the css styles but I could not get the background of the window to change (couldn't get the right selector inside of component template).我尝试使用 css 样式,但无法更改窗口的背景(无法在组件模板内获得正确的选择器)。

I went through the documentation but there is nothing there.我浏览了文档,但那里什么也没有。 I can play a little bit more with the styles since I am sure there is probably some tricky way but considering the darkening effect is already there out of the box I would assume there should be a theming feature available out of the box as well.我可以在样式上多玩一点,因为我确信可能有一些棘手的方法,但考虑到变暗效果已经开箱即用,我认为应该也应该有一个开箱即用的主题功能。 What you think?你认为呢?

在此处输入图像描述

I guess you've missed the property MatDialogConfig - backdropClass in the docs.我猜你已经错过了文档中的属性MatDialogConfig -backgroundClass

Check this StackBlitz DEMO for a simple example检查这个StackBlitz DEMO一个简单的例子

From this DEMO:从这个演示:

dialog-overview-example.ts:对话框概述-example.ts:

openDialog(): void {
  const dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
    width: '250px',
    data: {name: this.name, animal: this.animal},
    backdropClass: 'backdropBackground' // This is the "wanted" line
  });

  dialogRef.afterClosed().subscribe(result => {
    console.log('The dialog was closed');
    this.animal = result;
  });
}

styles.css:样式.css:

.backdropBackground {
  /* your css needs */
}

The given answer by @benshabatnoam is absolutely correct, but the documentation also has another option to disable the backdrop altogether. @benshabatnoam 给出的答案是绝对正确的,但文档还有另一个选项可以完全禁用背景。

hasBackdrop有背景

Here is an example:下面是一个例子:

https://stackblitz.com/edit/angular-ei9hdv https://stackblitz.com/edit/angular-ei9hdv

You can achieve a nice effect by combining opacity and blur.您可以通过组合不透明度和模糊来获得不错的效果。 Do like this:这样做:

Add backdropClass to your dialog-options:backdropClass添加到您的对话框选项中:

backdropClass: "bdrop"

And these rules to your stylesheet:并将这些规则添加到您的样式表中:

.bdrop {
    background-color: #bbbbbbf2;
    backdrop-filter: blur(4px);
}

Demo: https://angular-blurred-dialog-backdrop-zdyvpc.stackblitz.io/演示: https : //angular-blurred-dialog-backdrop-zdyvpc.stackblitz.io/

Also you can just overide class .mat-dialog-container {} in your styles.scss你也可以在你的styles.scss覆盖 class .mat-dialog-container {}

.mat-dialog-container {
  box-shadow: 0px 11px 15px -7px rgb(0 0 0 / 20%), 0px 24px 38px 3px rgb(0 0 0 / 14%), 0px 9px 46px 8px rgb(0 0 0 / 12%);
  background: #fff;
  color: black;
}
const dialogConfig = new MatDialogConfig();**strong text**
dialogConfig.id="dialog"
this.dialog.open(CreatOrderComponent,dialogConfig)

// you can give it an ID then you can use that id to style it // 你可以给它一个 ID 然后你可以使用这个 id 来设置它的样式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM