如何在 mat-tab-group 中为 Angular 更改过渡效果?

LeO*_*LeO 3 angular-material angular

Angular已经包含mat-tab-group了每个mat-tab动画效果。给定的动画效果很烦人,因此我想使用不同的动画效果。我已经一个fadeinfadeout从别人别人

export const fadeAnimation = trigger('fadeAnimation', [
  // The '* => *' will trigger the animation to change between any two states
  transition('* => *', [
    // The query function has three params.
    // First is the event, so this will apply on entering or when the element is added to the DOM.
    // Second is a list of styles or animations to apply.
    // Third we add a config object with optional set to true, this is to signal
    // angular that the animation may not apply as it may or may not be in the DOM.
    query(
      ':enter',
      [style({ opacity: 0 })],
      { optional: true }
    ),
    query(
      ':leave',
      // here we apply a style and use the animate function to apply the style over 0.3 seconds
      [style({ opacity: 1 }), animate('0.3s', style({ opacity: 0 }))],
      { optional: true }
    ),
    query(
      ':enter',
      [style({ opacity: 0 }), animate('0.3s', style({ opacity: 1 }))],
      { optional: true }
    )
  ])
]);
Run Code Online (Sandbox Code Playgroud)

尝试应用它

<mat-tab-group animationDuration="0ms">
  <mat-tab label="First"> 
      <div [@fadeAnimation]="'enter'">
           Content 1 
      </div>
  </mat-tab>
  <mat-tab label="Second"> Content 2 </mat-tab>
  <mat-tab label="Third"> Content 3 </mat-tab>
</mat-tab-group>
Run Code Online (Sandbox Code Playgroud)

失败。任何提示如何正确应用它?

根据@ysf 的建议更新了示例。

小智 10

我也不喜欢向左/向右滑动动画。你可以做这个我放在一起的变通方法淡入淡出动画。

您可以禁用 Angular Mat Tab 动画,然后在mat-tab-body-active类中添加一些 css 关键帧动画。

要禁用动画,您可以在 mat-tab 中添加它。

<mat-tab-group [@.disabled]="true">
Run Code Online (Sandbox Code Playgroud)

或者您可以在此处全局禁用它

禁用动画后,将其添加到 style.css 文件中

.mat-tab-body.mat-tab-body-active {
  animation: fade 0.5s;
}


@keyframes fade {
  0% {
    opacity: 0;
  }

  100% {
    opacity: 1;
  }
}
Run Code Online (Sandbox Code Playgroud)

这里检查我的例子


Kir*_*lov 5

考虑到您的代码,我猜您尝试淡入新选项卡,同时淡出前一个选项卡。

使用埃尔登的答案我建议进行一些细微的改变:

.mat-tab-body {
    animation: fade-out 0.5s;
    opacity: 0;
}
.mat-tab-body.mat-tab-body-active {
    animation: fade-in 0.5s;
    opacity: 1;
}

@keyframes fade-in {
    0% {
        opacity: 0;
    }
    50% {
        opacity: 0;
    }
    100% {
        opacity: 1;
    }
}

@keyframes fade-out {
    0% {
        opacity: 1;
    }
    50% {
        opacity: 0;
    }
    100% {
        opacity: 0;
    }
}
Run Code Online (Sandbox Code Playgroud)