Angular动画
animation
// 使用方法
git clone https://git.oschina.net/mumu-osc/learn-component.git
cd learn-component
git pull origin animation
npm install
ng serve
写法
// fly-in.ts
import { trigger, state, style, transition, animate, keyframes } from '@angular/animations';
export const flyIn = trigger('flyIn', [
transition('void => *', [
animate(3000, keyframes([
style({ opacity: 0, transform: 'translateX(-100%)', offset: 0 }),
style({ opacity: 1, transform: 'translateX(25px)', offset: 0.3 }),
style({ opacity: 1, transform: 'translateX(0)', offset: 1.0 })
]))
]),
transition('* => void', [
animate(300, keyframes([
style({ opacity: 1, transform: 'translateX(0)', offset: 0 }),
style({ opacity: 1, transform: 'translateX(-25px)', offset: 0.7 }),
style({ opacity: 0, transform: 'translateX(100%)', offset: 1.0 })
]))
])
]);
使用
<div class="panel panel-primary" [@flyIn]="state">
<div class="panel-heading">飞入效果</div>
<div class="panel-body">
飞入效果
</div>
</div>
import { Component, OnInit } from '@angular/core';
import { flyIn } from '../animations/fly-in';
@Component({
selector: 'app-test-fly-in',
templateUrl: './test-fly-in.component.html',
styleUrls: ['./test-fly-in.component.css'],
animations:[flyIn]
})
export class TestFlyInComponent implements OnInit {
state:string;
constructor() { }
ngOnInit() {
}
}
注意
需要在主模块中引入BrowserAnimationsModule
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { TestFlyInComponent } from './test-fly-in/test-fly-in.component';
@NgModule({
declarations: [
AppComponent,
TestFlyInComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }