我希望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;
}
更新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;
}
原版的
当表达式变为*ngIf
时,false
从DOM中删除元素。您不能在不存在的元素上进行转换。
请改用hidden
:
<div class="note" [ngClass]="{'transition':show}" [hidden]="!show">
根据最新的angular 2 documentation,您可以设置“进入和离开”元素的动画(如角度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时的状态(当元素未附加到视图时适用)。*
- 可以有许多动画状态(在文档中阅读更多)。 *
状态优先于所有这些状态作为“通配符”(在我的示例中,这是ngIf
设置为true
时的状态)。注意(取自角度文档):
在app模块import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
中额外声明
角度动画构建于标准Web动画API之上,并在支持它的浏览器上本机运行。对于其他浏览器,需要填充。从GitHub获取web-animations.min.js并将其添加到您的页面。
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;
}
我使用角度5和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%)'}))
])
])
];
一种方法是使用setter作为ngIf属性,并将状态设置为更新值的一部分。将属性设置为true时,需要调用detectChanges()以确保在动画状态更改之前将元素添加回dom。
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;
}