【问题标题】:2 different animations doesn't work together on 1 component2 个不同的动画不能在 1 个组件上一起使用
【发布时间】:2019-04-15 20:13:15
【问题描述】:

这是我的组件模板

<nav class="menu" [ngClass]="{ snap: snap }" [@menuBackground]="'normal'">
  <div class="container fluid">
    <div class="row align-items-center">
      <div class="col-2" [@logoZoomOut]="'normal'">
        <img *ngIf="true" src="../../../assets/logo.png" class="logo img-fluid" alt="logo" />
      </div>
      <div
        class="links-wrapper text-right d-none d-md-block col-md-8 offset-md-2 col-lg-6 offset-lg-4 col-xl-5 offset-xl-5"
      >
        <ul>
          <app-menu-link [link]="'/'" [text]="'Home'" [first]="true"></app-menu-link>
          <app-menu-link [link]="'/services'" [text]="'Services'"></app-menu-link>
          <app-menu-link [link]="'/portfolio'" [text]="'Portfolio'"></app-menu-link>
          <app-menu-link [link]="'/contact'" [text]="'Contact'" [contact]="true"></app-menu-link>
        </ul>
      </div>
    </div>
  </div>
</nav>

这是我的组件代码:

import { Component, OnInit, OnDestroy } from '@angular/core';
import { trigger, state, style, transition, animate } from '@angular/animations';

@Component({
  selector: 'app-menu',
  templateUrl: './menu.component.html',
  styleUrls: ['./menu.component.scss'],
  animations: [
    trigger('menuBackground', [
      state('normal', style({ background: 'rgba(118, 137, 169, 0.22)' })),
      transition('void => normal', [style({ background: 'none' }), animate('.5s 2s ease-in')])
    ]),
    trigger('logoZoomOut', [
      state('normal', style({ transform: 'scale(1)' })),
      transition('void => normal', [style({ transform: 'scale(1.5)' }), animate('.5s ease-in')])
    ])
  ]
})
export class MenuComponent implements OnInit, OnDestroy {
  public snap = false;

  constructor() {}

  ngOnInit() {
    window.addEventListener('scroll', this.scroll, true);
  }

  ngOnDestroy() {
    window.removeEventListener('scroll', this.scroll, true);
  }

  scroll = (): void => {
    if (window.pageYOffset > 90) {
      this.snap = true;
    } else {
      this.snap = false;
    }
  };
}

现在我想为导航的背景和徽标大小设置动画。 但问题是,当menuBackgroundlogoZoomOut 触发器都设置在navdiv 上时 - 只有背景动画并且不播放徽标的动画。

当我删除背景动画时 - 徽标动画正确。

那么我怎样才能同时为这两个元素设置动画呢? 我认为它应该独立工作。我也遇到了徽标动画的问题 - 如果没有 *ngIf="true" 上的 img 元素,它就无法工作。也许它有某种相关性?

【问题讨论】:

    标签: html angular typescript angular-template angular-animations


    【解决方案1】:

    使用animateChild可以实现触发内部动画。如果要同时触发两个动画,则需要额外使用group

    Stackblitz example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多