angular 2 ngIf and CSS transition/animation

前端 未结 7 1676
予麋鹿
予麋鹿 2020-11-29 16:23

I want a div to slide in from the right in angular 2 using css.

  

相关标签:
7条回答
  • 2020-11-29 17:04

    update 4.1.0

    Plunker

    See also https://github.com/angular/angular/blob/master/CHANGELOG.md#400-rc1-2017-02-24

    update 2.1.0

    Plunker

    For more details see Animations at angular.io

    import { trigger, style, animate, transition } from '@angular/animations';
    
    @Component({
      selector: 'my-app',
      animations: [
        trigger(
          'enterAnimation', [
            transition(':enter', [
              style({transform: 'translateX(100%)', opacity: 0}),
              animate('500ms', style({transform: 'translateX(0)', opacity: 1}))
            ]),
            transition(':leave', [
              style({transform: 'translateX(0)', opacity: 1}),
              animate('500ms', style({transform: 'translateX(100%)', opacity: 0}))
            ])
          ]
        )
      ],
      template: `
        <button (click)="show = !show">toggle show ({{show}})</button>
    
        <div *ngIf="show" [@enterAnimation]>xxx</div>
      `
    })
    export class App {
      show:boolean = false;
    }
    

    original

    *ngIf removes the element from the DOM when the expression becomes false. You can't have a transition on a non-existing element.

    Use instead hidden:

    <div class="note" [ngClass]="{'transition':show}" [hidden]="!show">
    
    0 讨论(0)
提交回复
热议问题