简体   繁体   English

如何使用 scss/sass 在关闭时为 MatDialog 设置动画?

[英]How to animate a MatDialog on close using scss/sass?

I am tending to override the scss-animations inside @angular/material/dialog .我倾向于覆盖@angular/material/dialog的 scss-animations 。 I have tried many ways and read many articles but I am still unable to reach to a solution.我尝试了很多方法并阅读了很多文章,但仍然无法找到解决方案。 When the MatDialog popup opens, it has its own animations but when I close it, it closes immediately (without animation ).当 MatDialog 弹出窗口打开时,它有自己的动画,但是当我关闭它时,它会立即关闭(没有animation )。

  • Firstly, the dialog opens by means of my NotifierService which looks like the following:首先,对话框通过我的NotifierService打开,如下所示:

notifier.service.ts


@Injectable()
export class NotifierService {
   ...
   
   constructor(
      private dialog: MatDialog,
      private dialogRef: MatDialogRef<OverlayComponent>
   ) { }

   ...
   
   public open(notification: Notification): void {
      this.dialogRef = this.dialog.open(NotificationComponent, {
         ...
         panelClass: 'notifications-popup',
         data: {
            notification: notification
         }
      });
   }
   ...

}
  • In my global scss file I have the following code:在我的全局 scss 文件中,我有以下代码:

styles.scss


...

@keyframes fadeOut {
   0% { width: 85%; height: 85vh; }
   100% { width: 0%; height: 0%; }
}

@keyframes fadeIn {
   0% { width: 0vh; height: 0vh; }
   100% { width: 85%; height: 85vh; }
}

...

div.notifications-popup {
  animation: fadeIn 0.15s forwards !important;
  animation-delay: 0.15s;
}

...

  • In my NotificationComponent I have the following above all:在我的NotificationComponent我首先拥有以下内容:

notification.component.ts

...
@Component({ ... })
export class NotificationComponent {
   ...

   public close(): void {
      this.fadeOutOnClose(); // Theoretically this should animate the closure of the dialog??????
      this.matDialogRef.close(this.data.overlay);
   }

   public fadeOutOnClose(): void {
      const view = document.getElementsByClassName('notifications-popup');
      for (let i = 0; i < view.length; i++) {
         this.renderer.setStyle(view[i], 'animation', 'fadeOut 1s forwards !important');
         this.renderer.setStyle(view[i], 'animation-delay', '1s');
      }
   }
   
   ...

}

...

Does anyone have any ideas about how to animate the closure of a MatDialog?有没有人对如何为 MatDialog 的关闭设置动画有任何想法?

Unfortunately, it's not an easy task.不幸的是,这不是一件容易的事。 That's why I chose this library for my projects:这就是我为我的项目选择这个库的原因:

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

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