1

I'm trying to apply angular 4 animation to list of html elements like

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>',
];

one by one with below code:

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%)'}))
    ])
  ])
]

When I put this trigger to list elements, all the elements come one the screen all in one go. I want to make them appear one by one. like fetch text 1 and then text 2 on html. How can I do that?

4

1 回答 1

1

更新:使用交错:

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 })
  ])
...

演示


它可以通过Angular 动画回调和一些用 typescript 编写的帮助来完成

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 来强调动画)

于 2017-08-20T13:35:48.060 回答