我试图将角度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。我怎么能这样做?
答案 0 :(得分: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 animation callbacks并使用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 (我用1000毫秒来强调动画)