如何使用 Angular 4 动画将动画逐一应用于元素列表?

问题描述 投票:0回答:1

我正在尝试将 Angular 4 动画应用于 html 元素列表,例如

facilities = ['<p> <span class="glyphicon glyphicon-hand-right"> </span> Text 1</p>',
        '<p> <span class="glyphicon glyphicon-hand-right"></span>Text 2</p>',
        '<p> <span class="glyphicon glyphicon-hand-right"></span> Text 3</p>',
        '<p> <span class="glyphicon glyphicon-hand-right"></span> Text 4</p>',
];

一一使用以下代码:

animations: [
  trigger('flyInOut', [
    state('in', style({transform: 'translateX(0)'})),
    transition('void => *', [
      style({transform: 'translateX(-100%)'}),
      animate(100)
    ]),
    transition('* => void', [
      animate(100, style({transform: 'translateX(100%)'}))
    ])
  ])
]

当我将此触发器放在列表元素上时,所有元素都会一次性出现在屏幕上。我想让它们一一出现。就像在 html 上获取文本 1,然后获取文本 2 一样。我怎样才能做到这一点?

javascript angular typescript angular-animations
1个回答
2
投票

更新: 使用交错:

HTML

<ul [@listAnimation]="items.length">
       <li *ngFor="let item of items" >
          <div [innerHTML]="item"></div>
       </li>
</ul>

打字稿:

....
animations: [
    trigger('listAnimation', [
   transition('* => *', [

     query(':leave', [
      stagger(500, [
        animate(1000, style({ opacity: 0 }))
      ])
    ], { optional: true }),
     query(':enter', [
           style({ opacity: 0 }),
 stagger(500, [
        animate(1000, style({ opacity: 1 }))
      ])
    ], { optional: true })
  ])
...

演示


可以通过角度动画回调以及用打字稿编写的一些帮助来完成

HTML:

 <ul>
    <li *ngFor="let item of listCopy;" (@flyInOut.done)="getNextItem()" [@flyInOut]="'in'" >
     <ul>
        <li *ngFor="let item of list" (@flyInOut.done)="getNextItem()" [@flyInOut]="'in'" >
           <div [innerHTML]="item"></div>
        </li>
     </ul>        
   </li>
 </ul>

打字稿:

   .....
   @Component({
   ....
   animations: [
      trigger('flyInOut', [
        state('in', style({transform: 'translateX(0)'})),
        transition('void => *', [
          style({transform: 'translateX(-100%)'}),
          animate(100)
        ]),
        transition('* => void', [
          animate(100, style({transform: 'translateX(100%)'}))
        ])
      ])
    ]
    ....
    list = ['Item1', 'Item2', 'Item3', 'Item4'];
    listCopy = []; 
    next: number = 0;

    constructor(){
        this.getNextItem();
    }

    getNextItem() {
        if(this.next < this.list.length) {
          this.listCopy.push(this.list[this.next++]);
        }
    }

Plunker(我放了1000ms来强调动画)

© www.soinside.com 2019 - 2024. All rights reserved.