当单击由* ngFor生成的特定div时,我想应用动画。当前,它适用于单击时的每个div。我该如何实现?
animations: [
trigger('changeDivSize', [
state('initial', style({
width: '30px',
height: '30px',
opacity: '0.5',
margin: '10px'
})),
state('final', style({
width: '32px',
height: '32px',
opacity: '1',
margin: '10px'
})),
transition('initial<=>final', animate('200ms')),
transition('final=>initial', animate('200ms'))
]),
]
changeState() {
this.currentState = this.currentState === 'initial' ? 'final' : 'initial';
}
<div class="row">
<div *ngFor="let group of groups" class="col-1">
<div (click)="changeState()" [@changeDivSize]=currentState [ngStyle]="{'background-color': group.colorCode}"></div>
</div>
答案 0 :(得分:2)
您可以将元素的索引与* ngFor一起使用,以使每个元素具有单独的currentState值,因此动画可以在单个元素上进行。
<div *ngFor="let group of groups;let i = index" class="col-1">
<div (click)="changeState(i)"
[@changeDivSize]=group.currentState
[ngStyle]="{'background-color': group.colorCode}">
</div>
</div>
public changeState(index: number): void {
console.log(index)
this.groups[index].currentState =
(this.groups[index].currentState === 'initial') ? 'final' : 'initial';
}
答案 1 :(得分:1)
changeState(index) {
this.currentState[index] = this.currentState[index] === 'initial' ? 'final' : 'initial';
}
更好地采用上述组中每个项目的当前状态数组。
然后将currentState [i]用于组的各个元素。
<div class="row">
<div *ngFor="let group of groups, index as i" class="col-1">
<div (click)="changeState(i)" [@changeDivSize]=currentState[i] [ngStyle]="{'background-color': group.colorCode}"></div>
</div>
希望这会有所帮助。