角度2 ngIf和CSS过渡/动画

时间:2016-04-05 05:36:31

标签: css angular angular-animations

我希望div使用css从角度2向右滑入。

  <div class="note" [ngClass]="{'transition':show}" *ngIf="show">
    <p> Notes</p>
  </div>
  <button class="btn btn-default" (click)="toggle(show)">Toggle</button>

如果我只使用[ngClass]切换类并使用不透明度,我的工作正常。 但李不希望从一开始就渲染这个元素,所以我先用ngIf“隐藏”它,但转换不会起作用。

.transition{
  -webkit-transition: opacity 1000ms ease-in-out,margin-left 500ms ease-in-out;
  -moz-transition: opacity 1000ms ease-in-out,margin-left 500ms ease-in-out;
  -ms-transition: opacity 1000ms ease-in-out,margin-left 500ms ease-in-out ;
  -o-transition: opacity 1000ms ease-in-out,margin-left 500ms ease-in-out;
  transition: opacity 1000ms ease-in-out,margin-left 500ms ease-in-out;
  margin-left: 1500px;
  width: 200px;
  opacity: 0;
}

.transition{
  opacity: 100;
  margin-left: 0;
}

7 个答案:

答案 0 :(得分:166)

更新4.1.0

Plunker

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

更新2.1.0

Plunker

有关详细信息,请参阅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;
}

<强>原始

当表达式变为*ngIf时,

false会从DOM中删除该元素。您不能在不存在的元素上进行转换。

改为使用hidden

<div class="note" [ngClass]="{'transition':show}" [hidden]="!show">

答案 1 :(得分:111)

根据最新的angular 2 documentation ,您可以制作动画&#34;进入和退出&#34;元素(如角度1)。

简单淡入淡出动画示例:

在相关的@Component中添加:

animations: [
  trigger('fadeInOut', [
    transition(':enter', [   // :enter is alias to 'void => *'
      style({opacity:0}),
      animate(500, style({opacity:1})) 
    ]),
    transition(':leave', [   // :leave is alias to '* => void'
      animate(500, style({opacity:0})) 
    ])
  ])
]

不要忘记添加导入

import {style, state, animate, transition, trigger} from '@angular/animations';

相关组件的html元素应如下所示:

<div *ngIf="toggle" [@fadeInOut]>element</div>

我构建了幻灯片和淡入淡出动画的示例 here

解释 on&#39; void&#39;和&#39; *&#39;:

  • voidngIf设置为false时的状态(适用于。{ 元素未附加到视图上。)
  • * - 可以有许多动画状态(在文档中阅读更多内容)。 *状态优先于所有状态作为&#34;通配符&#34; (在我的示例中,这是ngIf设置为true时的状态。)

注意(取自角文档):

额外在app模块中声明, import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

  

角度动画构建于标准Web动画API之上   并在支持它的浏览器上本机运行。对于其他浏览器,a   polyfill是必需的。从GitHub获取web-animations.min.js并添加   它到你的页面。

答案 2 :(得分:14)

trigger('slideIn', [
    state('*', style({ 'overflow-y': 'hidden' })),
    state('void', style({ 'overflow-y': 'hidden' })),
    transition('* => void', [
        style({ height: '*' }),
        animate(250, style({ height: 0 }))
    ]),
    transition('void => *', [
        style({ height: '0' }),
        animate(250, style({ height: '*' }))
])

答案 3 :(得分:9)

现代浏览器的CSS唯一解决方案

@keyframes slidein {
    0%   {margin-left:1500px;}
    100% {margin-left:0px;}
}
.note {
    animation-name: slidein;
    animation-duration: .9s;
    display: block;
}

答案 4 :(得分:3)

我使用angular 5和ngif为我工作,我必须使用animateChild,在用户详细信息组件中,我使用了* ngIf =&#34; user.expanded&#34;显示隐藏用户并且它可用于输入

 <div *ngFor="let user of users" @flyInParent>
  <ly-user-detail [user]= "user" @flyIn></user-detail>
</div>

//the animation file


export const FLIP_TRANSITION = [ 
trigger('flyInParent', [
    transition(':enter, :leave', [
      query('@*', animateChild())
    ])
  ]),
  trigger('flyIn', [
    state('void', style({width: '100%', height: '100%'})),
    state('*', style({width: '100%', height: '100%'})),
    transition(':enter', [
      style({
        transform: 'translateY(100%)',
        position: 'fixed'
      }),
      animate('0.5s cubic-bezier(0.35, 0, 0.25, 1)', style({transform: 'translateY(0%)'}))
    ]),
    transition(':leave', [
      style({
        transform: 'translateY(0%)',
        position: 'fixed'
      }),
      animate('0.5s cubic-bezier(0.35, 0, 0.25, 1)', style({transform: 'translateY(100%)'}))
    ])
  ])
];

答案 5 :(得分:2)

一种方法是使用setter作为ngIf属性,并将状态设置为更新值的一部分。将属性设置为true时,需要调用detectChanges()以确保在动画状态发生变化之前将元素添加回dom。

StackBlitz example

<强> example.component.ts

import { Component, AnimationTransitionEvent, OnInit } from '@angular/core';
import { trigger, state, style, animate, transition } from '@angular/animations';

@Component({
  selector: 'example',
  templateUrl: `./example.component.html`,
  styleUrls: [`./example.component.css`],
  animations: [
    trigger('state', [
      state('visible', style({
        opacity: '1'
      })),
      state('hidden', style({
        opacity: '0'
      })),
      transition('* => visible', [
        animate('500ms ease-out')
      ]),
      transition('visible => hidden', [
        animate('500ms ease-out')
      ])
    ])
  ]
})
export class ExampleComponent implements OnInit  {
  state: string;

    private _showButton: boolean;
    get showButton() {
      return this._showButton;
    }
    set showButton(val: boolean) {
      if (val) {
        this._showButton = true;
        this.state = 'visible';
      } else {
        this.state = 'hidden';
      }
    }

    constructor() {
    }

    ngOnInit() {
      this.showButton = true;
    }

    animationDone(event: AnimationTransitionEvent) {
      if (event.fromState === 'visible' && event.toState === 'hidden') {
        this._showButton = false;
      }
    }

    log() {
      console.log('clicked');
    }
}

<强> example.component.html

<div>
  <p>animation state: {{state}}</p>
  <p>showButton: {{showButton}}</p>
  <button (click)="showButton = !showButton">toggle</button>
</div>
<button class="animation-target" *ngIf="showButton" [@state]="state" (@state.done)="animationDone($event)" (click)="log()" >animation target</button>

<强> example.component.css

.animation-target {
    background: orange;
    height: 150px;
    width: 150px;
    cursor: pointer;
    opacity: 0;
}

答案 6 :(得分:0)

就我而言,我错误地在错误的组件上声明了动画。

app.component.html

  <app-order-details *ngIf="orderDetails" [@fadeInOut] [orderDetails]="orderDetails">
  </app-order-details>

需要在(appComponent.ts)中使用元素的组件上声明动画。我是在OrderDetailsComponent.ts上声明动画。

希望它能帮助犯同样错误的人