30
loading...
This website collects cookies to deliver better user experience
*ngIf
and *ngFor
will remove elements from the DOM. There isn’t a CSS solution for animating a non-existing element in the DOM. But Angular provides us with a simple solution.*ngIf
it is equally applicable to *ngFor
. The complete, working code can be downloaded here.ng new ngifAnimation
*ngIf
on the image. Open app.component.html
and add a simple button: (this is the default HTML)<!--The content below is only a placeholder and can be replaced.--
<div style="text-align:center">
<h1>Welcome to {{ title }}!</h1>
<button (click)="onClick()">Toggle Image</button>
...
onClick()
method to the class that toggles a public variable showImage
:export class AppComponent {
title = 'ngifAnimation';
showImage = false;
onClick() {
this.showImage = !this.showImage;
}
}
*ngIf
in the template on the <img>
tag:<img
*ngIf="showImage"
width="300"
alt="Angular Logo"
src="..."
/>
app.component.scss
)button {
display: block;
margin: 0 auto;
clear: both;
}
<img>
tag is popping in and out of the DOM. When showImage
is false the <img>
tag isn’t even present. This is where our inability to use CSS comes into play. It’s a terrible user experience to have elements, especially large ones, pop in and out without some transition. Let’s make it grow and fade in and out in a pleasing manner!BrowserAnimationsModule
. As of the latest Visual Studio Code, though, it doesn’t want to auto-import this module for you if you add it to your AppModule
imports. It’s hidden in @angular/platform-browser/animations
. Let’s add the import manually and add it to the module’s imports.import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, BrowserAnimationsModule],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
app.component.ts's
@Component
directive. Here is the whole thing, but don’t worry, we’ll break it down and explain it.import { trigger, state, style, animate, transition } from '@angular/animations';
@Component({
...,
animations: [
trigger(
'inOutAnimation',
[
transition(
':enter',
[
style({ height: 0, opacity: 0 }),
animate('1s ease-out',
style({ height: 300, opacity: 1 }))
]
),
transition(
':leave',
[
style({ height: 300, opacity: 1 }),
animate('1s ease-in',
style({ height: 0, opacity: 0 }))
]
)
]
)
]
})
animations: []
is an array of things we want to happen or state definitions. In this case we just want to trigger
an animation called inOutAnimation
. You can name this what you like. It should be descriptive for what it accomplishes or what it should consistently apply to. In our case we are animating an image in and out of the view.trigger
a set of states and/or transitions. We only need two specific transitions to occur that are related to *ngIf
: :enter
and :leave
. These are the states that CSS just doesn’t give us. :enter
is when a DOM element is being added, and :leave
is when a DOM element is being removed.:enter
we are starting with the style of height: 0, opacity: 0
. It’s basically invisible to start with. When it’s done we would like it to be 300 pixels tall and be completely opaque.animate
instruction comes in. We are going to animate over 1) a period of time 2) with a particular easing mechanism 3) to a new style. 1 and 2 are combined in the first string-based instruction, 0.3s ease-out
. This means that we are animating to the new style over 0.3 seconds, and we are easing out, or coming to a gentle stop rather than a sudden one. 3 specifies what the end styling should be. In our case that’s 300 pixels high and completely opaque.<img>
tag that has the *ngIf
directive on it.<img
*ngIf="showImage"
[@inOutAnimation]
width="300"
alt="Angular Logo"
src="..."
/>
[]
's, which makes me scratch my head a bit.Angular’s animation system lets you build animations that run with the same kind of native performance found in pure CSS animations. You can also tightly integrate your animation logic with the rest of your application code, for ease of control.