您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

角度2 ngIf和CSS过渡/动画

角度2 ngIf和CSS过渡/动画

另请参见https://github.com/angular/angular/blob/master/CHANGELOG.md#400-rc1-2017-02-24

有关更多详细信息,请参见

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;
}

*ngIf当表达式变为时,从DOM中删除元素false。您不能在不存在的元素上进行过渡。

改为使用hidden

<div class="note" [ngClass]="{'transition':show}" [hidden]="!show">
CSS 2022/1/1 18:22:31 有313人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶