角度2 ngIf和CSS过渡/动画

我希望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]来切换类并利用不透明性,则可以正常工作。但是li不想从一开始就渲染该元素,因此我先用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;

}

回答:

另请参见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">

以上是 角度2 ngIf和CSS过渡/动画 的全部内容, 来源链接: utcz.com/qa/426817.html

回到顶部