Angular - restart animation by event

Viewed 24
1 Answers

you can do with css animations.

.css :

@keyframes anim {
  from { background-color: red; }
  to { background-color: ''; }
}
.color-animation {
  animation-name: anim;
  animation-duration: 1s;
}

.html :

<div #textContainer (click)="launchAnim(textContainer)">
  <p>Click me!</p>
</div>

.ts :

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {

  public launchAnim(textContainer: HTMLDivElement) {
    textContainer.classList.toggle('color-animation');
    textContainer.offsetHeight; // force DOM reflow
    textContainer.className = 'color-animation';
  }
}

For more information about DOM reflow see here

Related