I am creating a simple, single page site (mostly to learn Angular), and cant seem to figure out how to use a single animation to affect different DOM elements. I could define the animation for each element, but that seems extremely ineffective.
Is there a way to animate the image within the clicked button without defining a separate animation block for each element?
Thank you, Terry
<!-- HTML: -->
<button mat-flat-button (click)="finishedChore()">
<img
[@openClose]="isOpen ? 'open' : 'closed'"
src="assets/images/morning.png"
/>
</button>
<button mat-flat-button (click)="finishedChore()">
<img
[@openClose]="isOpen ? 'open' : 'closed'"
src="assets/images/poop.png"
/>
</button>
<button mat-flat-button (click)="finishedChore()">
<img
[@openClose]="isOpen ? 'open' : 'closed'"
src="assets/images/cleanRoom.png"
/>
</button>
<button mat-flat-button (click)="finishedChore()">
<img
[@openClose]="isOpen ? 'open' : 'closed'"
src="assets/images/cleanSinks.png"
/>
</button>
<button mat-flat-button (click)="finishedChore()">
<img
[@openClose]="isOpen ? 'open' : 'closed'"
src="assets/images/evening.png"
/>
</button>
// .ts file
import { Component, OnInit } from '@angular/core';
import {
trigger,
state,
style,
animate,
transition,
} from '@angular/animations';
@Component({
selector: 'app-chore-list',
templateUrl: './chore-list.component.html',
styleUrls: ['./chore-list.component.scss'],
animations: [
trigger('openClose', [
state('closed', style({ backgroundColor: '' })),
state('open', style({ backgroundColor: 'blue' })),
transition('closed<=>open', [animate('0.3s 0.0s ease-in')]),
]),
],
})
export class ChoreListComponent implements OnInit {
isOpen = false;
constructor() {}
ngOnInit(): void {}
finishedChore() {
this.isOpen = !this.isOpen;
}
}