目录

Angular 4 – Animations

动画在html元素之间添加了很多交互。 Angular2也提供动画。 与Angular 4的不同之处在于动画不再是@angular/core库的一部分,而是一个需要在app.module.ts导入的独立包。

首先,我们需要按如下方式导入库 -

import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

需要将BrowserAnimationsModule添加到app.module.ts的导入数组中,如下所示 -

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
@NgModule({
   declarations: [
      AppComponent
   ],
   imports: [
      BrowserModule,
      BrowserAnimationsModule
   ],
   providers: [],
   bootstrap: [AppComponent]
})
export class AppModule { }

app.component.html ,我们添加了要动画的html元素。

<div>
   <button (click)="animate()">Click Me</button>
   <div [@myanimation] = "state" class="rotate">
      <img src="assets/images/img.png" width="100" height="100">
   </div>
</div>

对于主div,我们添加了一个按钮和一个带图像的div。 有一个click事件,调用animate函数。 对于div,添加@myanimation指令并将值赋予状态。

现在让我们看一下定义动画的app.component.ts

import { Component } from '@angular/core';
import { trigger, state, style, transition, animate } from '@angular/animations';
@Component({
   selector: 'app-root',
   templateUrl: './app.component.html',
   styleUrls: ['./app.component.css'],
   styles:[`
      div{
         margin: 0 auto;
         text-align: center;
         width:200px;
      }
      .rotate{
         width:100px;
         height:100px;
         border:solid 1px red;
      }
   `],
   animations: [
      trigger('myanimation',[
         state('smaller',style({
            transform : 'translateY(100px)'
         })),
         state('larger',style({
            transform : 'translateY(0px)'
         })),
         transition('smaller <=> larger',animate('300ms ease-in'))
      ])
   ]
})
export class AppComponent {
   state: string = "smaller";
   animate() {
      this.state= this.state == 'larger' ? 'smaller' : 'larger';
   }
}

我们必须导入要在.ts文件中使用的动画功能,如上所示。

import { trigger, state, style, transition, animate } from '@angular/animations';

这里我们从@ angular/animations导入了触发器,状态,样式,过渡和动画。

现在,我们将动画属性添加到@Component()装饰器 -

animations: [
   trigger('myanimation',[
      state('smaller',style({
         transform : 'translateY(100px)'
      })),
      state('larger',style({
         transform : 'translateY(0px)'
      })),
      transition('smaller <=> larger',animate('300ms ease-in'))
   ])
]

触发器定义动画的开始。 它的第一个参数是要赋予动画需要应用的html标签的动画名称。 第二个参数是我们导入的函数 - 状态,转换等。

state函数涉及动画步骤,元素将在它们之间转换。 现在我们已经定义了两个状态,越来越小。 对于较小的状态,我们给出了样式transform:translateY(100px)transform:translateY(100px)

Transition函数将动画添加到html元素。 第一个参数采用状态,即开始和结束; 第二个参数接受animate函数。 动画功能允许您定义过渡的长度,延迟和缓和。

现在让我们看一下.html文件,看看过渡函数是如何工作的

<div>
   <button (click)="animate()">Click Me</button>
   <div [@myanimation] = "state" class="rotate">
      <img src="assets/images/img.png" width="100" height="100">
   </div>
</div>

@component指令中添加了一个样式属性,它集中对齐div。 让我们考虑下面的例子来理解相同的 -

styles:[`
   div{
      margin: 0 auto;
      text-align: center;
      width:200px;
   }
   .rotate{
      width:100px;
      height:100px;
      border:solid 1px red;
   }
`],

这里,一个特殊的字符[``]用于向html元素添加样式(如果有的话)。 对于div,我们给出了app.component.ts文件中定义的动画名称。

在单击按钮时,它调用animate函数,该函数在app.component.ts文件中定义如下 -

export class AppComponent {
   state: string = "smaller";
   animate() {
      this.state= this.state == ‘larger’? 'smaller' : 'larger';
   }
}

定义状态变量,并将其默认值设置为较小。 animate函数会在单击时更改状态。 如果状态更大,它将转换为更小; 如果更小,它将转换为更大。

这是浏览器中的输出( http://localhost:4200/ )的样子 -

单击我按钮

单击“ Click Me按钮后,图像的位置将更改,如以下屏幕截图所示 -

单击我按钮图像位置已更改

变换函数应用于y方向,单击Click Me按钮时,该方向从0变为100px。 图像存储在assets/images文件夹中。

↑回到顶部↑
WIKI教程 @2018