angular 2 ngIf and CSS transition/animation
According to the latest angular 2 documentation you can animate "Entering and Leaving" elements (like in angular 1).
Example of simple fade animation:
In relevant @Component add:
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}))
])
])
]
Do not forget to add imports
import {style, state, animate, transition, trigger} from '@angular/animations';
The relevant component's html's element should look like:
<div *ngIf="toggle" [@fadeInOut]>element</div>
I built example of slide and fade animation here.
Explanation on 'void' and '*':
void
is the state whenngIf
is set to false (it applies when the element is not attached to a view).*
- There can be many animation states (read more in docs). The*
state takes precedence over all of them as a "wildcard" (in my example this is the state whenngIf
is set totrue
).
Notice (taken from angular docs):
Extra declare inside the app module,
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: '*' }))
])
])
update 4.1.0
Plunker
See also https://github.com/angular/angular/blob/master/CHANGELOG.md#400-rc1-2017-02-24
update 2.1.0
Plunker
For more details see 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;
}
original
*ngIf
removes the element from the DOM when the expression becomes false
. You can't have a transition on a non-existing element.
Use instead hidden
:
<div class="note" [ngClass]="{'transition':show}" [hidden]="!show">