Animation for newly created elements, but not on page load

I subscribed to the Firebase database in real time, so when I transfer something, it immediately displays in the view without the need for jQuery or ajax.

I would like to animate the rendering of such elements, so when a new element is added to the DOM, its div background-colorgreen and slowly disappear. I do not want all of divthis class to execute this animation at boot time.

I know how to do the first:

@keyframes green-fade {
    0% {background: rgb(173, 235, 173);}
    100% {background: none;}
}

.post-div {
   animation: green-fade 5s ease-in 1;
}

But, of course, this animation happens for this class whenever it is displayed, including when loading.

I'm interested in "Angular 2 way".

+4
4

Angular 4.25 : : , :

template: `
  <div [@preventInitialChildAnimations]>
    <div [@someAnimation]>...</div>
  </div>
`,
animations: [
  trigger('preventInitialChildAnimations', [
    transition(':enter', [
      query(':enter', [], {optional: true})
    ])
  ]),
  ...
]
+5

, , . , itemState new.

    trigger('itemState', [
      transition('* => new', animate(5000, keyframes([
        style({ backgroundColor: 'red', offset: 0 }),
        style({ backgroundColor: '*', offset: 1.0 })
      ]))),

null, .

<div [@itemState]="someItem.itemState" (@itemState.done)="someItem.itemState=''">

itemState , !

+3

AfterViewInit .

https://embed.plnkr.co/5l1kf5lMLEXSE8pNzqik/

@Component({
  selector: 'my-app',
  template: `
    <div *ngFor="let item of items" [@greenFade]="animate ? 'in' : null">
      {{item}}
    </div>

    <button (click)="addItem()">add</button>
  `,
  animations: [
    trigger('greenFade', [
      transition('void => in', [style({background: 'rgb(173, 235, 173)'}), animate('5s ease-in')])
    ])
  ]
})
class App implements AfterViewInit {
  constructor(private cdRef: ChangeDetectorRef){}

  items: String = ['Item','Item','Item'];
  addItem(){
    this.items.push('Item');
  }

  animate: boolean;
  ngAfterViewInit(){
    this.animate = true;
    this.cdRef.detectChanges();
  }
}
+2

:

 @Component({
    selector: 'myComponent',
    template: '<div [@.disabled]="disableAnimations" [@someAnimation]="someValue">',
    animations: [trigger('someAnimation', [transition('* => *', [style({ transform: 'scale(1.1)' }), animate(250)])])]
})
export class MyComponent implements AfterViewInit {

    disableAnimations: boolean = true;

    constructor() {}

    ngAfterViewInit(): void {
        this.disableAnimations = false;
    }
}

Link: https://angular.io/api/animations/trigger (scroll to "disable animation")

0
source

Source: https://habr.com/ru/post/1672515/


All Articles