ngFor 的转换延迟角度 4

ami*_*eli 2 javascript animation angular angular-animations

我正在尝试为ngFor.

每个项目必须在前一个项目之后几毫秒进行动画处理。

这是我的组件代码:

@Component({
    selector: 'bookmark-list',
    templateUrl: './bookmark.list.component.html',
    providers: [BookmarkService],
    styleUrls: ['./bookmark.list.component.less'],
    animations: [
        trigger('myAwesomeAnimation', [
            transition(':enter', [
                style({transform: 'scale(0.8)',}),
                animate('1.5s ease-out', style({transform: 'scale(1)',})),
            ]),
        ])
    ]
})
Run Code Online (Sandbox Code Playgroud)

以及它的 html 标记:

<div class="col-sm-6 col-md-4 col-lg-3" *ngFor="let bookmark of bookmarks | bookmarkPipe:search">
  <div [@myAwesomeAnimation]='"large"'>
    <bookmark-item [bookmark]="bookmark"></bookmark-item>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

有没有办法将延迟作为角度转换的参数传递?

编辑

根据 Oluwafemi Sule 的回答,交错是解决方案:

    transition('* => *', [
        query(':leave', [
          stagger(
            100, [
              animate('0s ease-in-out', style({
                transform: 'scale(0.8)'
              }))
            ]
          )
        ], {optional: true}),
        query(':enter', [
          style({
            transform: 'scale(0.8)',
          }),
          stagger(100, [
            animate('0.5s 0.7s ease-in-out', style({
              transform: 'scale(1)',
            }))
          ])
        ], {optional: true})
      ])
Run Code Online (Sandbox Code Playgroud)

必须改进过渡,但它可以完成工作。

和 HTML 标记:

<section class="row" [@myAwesomeAnimation]='bookmarks.length'>
    <div class="col-sm-6 col-md-4 col-lg-3" *ngFor="let bookmark of bookmarks | bookmarkPipe:search">
      <bookmark-item [bookmark]="bookmark"></bookmark-item>
    </div>
  </section>
Run Code Online (Sandbox Code Playgroud)

Olu*_*ule 7

您可以在持续时间之后传递延迟。

animate('1.5s delay ease-out', style({transform: 'scale(1)',})),
Run Code Online (Sandbox Code Playgroud)

您需要为列表中的每个项目计算延迟以实现平滑过渡。

为了实现每个项目的延迟,您需要升级到4.2.0-4.3.2 版本中Angular 版本才能使用实验 stagger动画功能。

trigger('myAwesomeAnimation', [
  transition(':enter', [
    query(':leave', [
      style({transform: 'scale(0.8)',}),
      stagger(100, [
        animate('1.5s ease-out', style({transform: 'scale(1)',})),
      ])
    ]),
    ...
  ]),
])
Run Code Online (Sandbox Code Playgroud)

参考: