你不需要材料,因为Angular具有内置的动画效果。以下是一个例子:
import { Component } from '@angular/core';
import { trigger, state, style, animate, transition } from '@angular/animations';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
animations: [
trigger('rotatedState', [
state('default', style({ transform: 'rotate(0)' })),
state('rotated', style({ transform: 'rotate(-180deg)' })),
transition('rotated => default', animate('1500ms ease-out')),
transition('default => rotated', animate('400ms ease-in'))
])
]
})
export class AppComponent {
state: string = 'default';
rotate() {
this.state = (this.state === 'default' ? 'rotated' : 'default');
}
}
并且在模板中:
<button (click)="rotate()">Press me to rotate</button>
确保将绑定添加到您正在操作的标记:
<img [@rotatedState]='state' />
此外,确保像这样将动画模块导入到您的应用程序中:
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
@NgModule({
...,
imports: [
...,
BrowserAnimationsModule
],
...
})
查看带有工作示例的StackBlitz