Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Animation which creates short highlight

Problem

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:

Temporary Solution

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 = ''">

StackBlitz Demo

Question

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 ?

Versions

Angular 7

like image 443
Joniras Avatar asked Oct 18 '18 14:10

Joniras


People also ask

What is the benefit of Angular animation?

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.

Which animation strategy would you use to apply multiple styles for a transition?

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.


1 Answers

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

like image 73
Eliseo Avatar answered Sep 19 '22 15:09

Eliseo