I recently wanted to shortly highlight an Element with Angular animations. But i did not find a way to do it without a state. So here is what i came up with:
animations: [
trigger('highlightRed', [
transition('*=>hasError', animate('2000ms', keyframes([
style({backgroundColor: 'initial', boxShadow: 'none', offset: 0} ),
style({backgroundColor: '#ff5c4c', boxShadow: '0 0 5px #ff5c4c', offset: 0.1} ),
style({backgroundColor: 'initial', boxShadow: 'none', offset: 1} ),
])))
])
]
...
...
public showError(){
this.errorState = "hasError";
}
<span [@highlightRed]="errorState" (@highlightRed.done)="errorState = ''">
Is this type of animation even possible with Angular (-Animations) or do i have to use old-school css animations and how would i trigger them ideally ?
Angular 7
Animation provides the illusion of motion: HTML elements change styling over time. Well-designed animations can make your application more fun and straightforward to use, but they aren't just cosmetic.
The keyframes() function in Angular allows you to specify multiple interim styles within a single transition, with an optional offset to define the point in the animation where each style change should occur.
I don't know much about transitions Angular, but I understand that Angular trigger an animation when "something" change. Something change, when some is displayed or when a variable change.
Some like
<div [@highlightRed] >..</div>
transition('void=>*', animate(2000,, keyframes([..])),
create a transition at first of the component (or if you have a *ngIf="condition")
Some like
<div [@highlightRed]="value" >..</div>
transition('void=>*', animate(0)),
transition('*=>*', animate(2000,, keyframes([..])),
trigger the animation if you make a
<button (click)="value=!value">click</button>
See that you needn't declare "value" in .ts
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With