angular 2 ngIf 和 CSS transition/animation
angular 2 ngIf and CSS transition/animation
我想要 div 使用 css 从 angular 2 中的右侧滑入。
<div class="note" [ngClass]="{'transition':show}" *ngIf="show">
<p> Notes</p>
</div>
<button class="btn btn-default" (click)="toggle(show)">Toggle</button>
如果我只使用 [ngClass] 切换 class 并利用不透明度,我可以正常工作。
但是 li 不希望从一开始就渲染该元素,所以我 "hide" 首先用 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;
}
更新 4.1.0
另见 https://github.com/angular/angular/blob/master/CHANGELOG.md#400-rc1-2017-02-24
更新 2.1.0
有关详细信息,请参阅 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;
}
原创
当表达式变为 false
时,*ngIf
从 DOM 中删除元素。不能对不存在的元素进行过渡。
改用hidden
:
<div class="note" [ngClass]="{'transition':show}" [hidden]="!show">
根据最新的 angular 2 documentation 您可以为 "Entering and Leaving" 个元素设置动画(如 angular 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。
对 'void' 和 '*' 的解释:
void
是ngIf
设置为false时的状态(适用于
元素未附加到视图)。
*
- 可以有很多动画状态(在文档中阅读更多内容)。 *
状态作为 "wildcard" 优先于所有状态(在我的示例中,这是 ngIf
设置为 true
时的状态)。
通知(摘自angular 文档):
Extra 在应用模块中声明,
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
Angular animations are built on top of the standard Web Animations API
and run natively on browsers that support it. For other browsers, a
polyfill is required. Grab web-animations.min.js from GitHub and add
it to your page.
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: '*' }))
])
])
CSS 现代浏览器的唯一解决方案
@keyframes slidein {
0% {margin-left:1500px;}
100% {margin-left:0px;}
}
.note {
animation-name: slidein;
animation-duration: .9s;
display: block;
}
我正在使用 angular 5 并且为了让 ngfor 中的 ngif 为我工作,我不得不使用 animateChild 并且在用户详细信息组件中我使用了 *ngIf="user.expanded"显示隐藏用户并且它适用于输入离开
<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%)'}))
])
])
];
一种方法是对 ngIf 属性 使用 setter 并将状态设置为更新值的一部分。
fade.component.ts
import {
animate,
AnimationEvent,
state,
style,
transition,
trigger
} from '@angular/animations';
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
export type FadeState = 'visible' | 'hidden';
@Component({
selector: 'app-fade',
templateUrl: './fade.component.html',
styleUrls: ['./fade.component.scss'],
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')])
])
],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class FadeComponent {
state: FadeState;
// tslint:disable-next-line: variable-name
private _show: boolean;
get show() {
return this._show;
}
@Input()
set show(value: boolean) {
if (value) {
this._show = value;
this.state = 'visible';
} else {
this.state = 'hidden';
}
}
animationDone(event: AnimationEvent) {
if (event.fromState === 'visible' && event.toState === 'hidden') {
this._show = false;
}
}
}
fade.component.html
<div
*ngIf="show"
class="fade"
[@state]="state"
(@state.done)="animationDone($event)"
>
<button mat-raised-button color="primary">test</button>
</div>
example.component.css
:host {
display: block;
}
.fade {
opacity: 0;
}
在我的例子中,我错误地在错误的组件上声明了动画。
app.component.html
<app-order-details *ngIf="orderDetails" [@fadeInOut] [orderDetails]="orderDetails">
</app-order-details>
需要在(appComponent.ts
)中使用元素的组件上声明动画。我改为在 OrderDetailsComponent.ts
上声明动画。
希望对犯同样错误的人有所帮助
我想要 div 使用 css 从 angular 2 中的右侧滑入。
<div class="note" [ngClass]="{'transition':show}" *ngIf="show">
<p> Notes</p>
</div>
<button class="btn btn-default" (click)="toggle(show)">Toggle</button>
如果我只使用 [ngClass] 切换 class 并利用不透明度,我可以正常工作。 但是 li 不希望从一开始就渲染该元素,所以我 "hide" 首先用 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;
}
更新 4.1.0
另见 https://github.com/angular/angular/blob/master/CHANGELOG.md#400-rc1-2017-02-24
更新 2.1.0
有关详细信息,请参阅 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;
}
原创
当表达式变为false
时,*ngIf
从 DOM 中删除元素。不能对不存在的元素进行过渡。
改用hidden
:
<div class="note" [ngClass]="{'transition':show}" [hidden]="!show">
根据最新的 angular 2 documentation 您可以为 "Entering and Leaving" 个元素设置动画(如 angular 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。
对 'void' 和 '*' 的解释:
void
是ngIf
设置为false时的状态(适用于 元素未附加到视图)。*
- 可以有很多动画状态(在文档中阅读更多内容)。*
状态作为 "wildcard" 优先于所有状态(在我的示例中,这是ngIf
设置为true
时的状态)。
通知(摘自angular 文档):
Extra 在应用模块中声明,
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
Angular animations are built on top of the standard Web Animations API and run natively on browsers that support it. For other browsers, a polyfill is required. Grab web-animations.min.js from GitHub and add it to your page.
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: '*' }))
])
])
CSS 现代浏览器的唯一解决方案
@keyframes slidein {
0% {margin-left:1500px;}
100% {margin-left:0px;}
}
.note {
animation-name: slidein;
animation-duration: .9s;
display: block;
}
我正在使用 angular 5 并且为了让 ngfor 中的 ngif 为我工作,我不得不使用 animateChild 并且在用户详细信息组件中我使用了 *ngIf="user.expanded"显示隐藏用户并且它适用于输入离开
<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%)'}))
])
])
];
一种方法是对 ngIf 属性 使用 setter 并将状态设置为更新值的一部分。
fade.component.ts
import {
animate,
AnimationEvent,
state,
style,
transition,
trigger
} from '@angular/animations';
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
export type FadeState = 'visible' | 'hidden';
@Component({
selector: 'app-fade',
templateUrl: './fade.component.html',
styleUrls: ['./fade.component.scss'],
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')])
])
],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class FadeComponent {
state: FadeState;
// tslint:disable-next-line: variable-name
private _show: boolean;
get show() {
return this._show;
}
@Input()
set show(value: boolean) {
if (value) {
this._show = value;
this.state = 'visible';
} else {
this.state = 'hidden';
}
}
animationDone(event: AnimationEvent) {
if (event.fromState === 'visible' && event.toState === 'hidden') {
this._show = false;
}
}
}
fade.component.html
<div
*ngIf="show"
class="fade"
[@state]="state"
(@state.done)="animationDone($event)"
>
<button mat-raised-button color="primary">test</button>
</div>
example.component.css
:host {
display: block;
}
.fade {
opacity: 0;
}
在我的例子中,我错误地在错误的组件上声明了动画。
app.component.html
<app-order-details *ngIf="orderDetails" [@fadeInOut] [orderDetails]="orderDetails">
</app-order-details>
需要在(appComponent.ts
)中使用元素的组件上声明动画。我改为在 OrderDetailsComponent.ts
上声明动画。
希望对犯同样错误的人有所帮助