角度动画:旋转180°单击图像

Nao*_*fix 5 html angular angular-animations

如何使用Angular 4在图像按钮上进行图像旋转(单击:0° - > 180°/再次单击:180° - > 0°)?

我也使用Angular Material.

这是我的按钮:

 <!-- Button open left menubar -->
<button md-icon-button class="left-menubar-button"  (click)="start.toggle()">
  <md-icon class="md-custom-theme-gray">label_outline</md-icon>
</button>
Run Code Online (Sandbox Code Playgroud)

这个按钮会打开一个sidenav,我想动画它以获得更多的用户友好性

谢谢

FRE*_*CIA 19

编辑检查这个stackblitz工作示例

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

@Component({
    selector: 'my-app',
    animations: [
          // Each unique animation requires its own trigger. The first argument of the trigger function is the name
          trigger('rotatedState', [
              state('default', style({ transform: 'rotate(0)' })),
              state('rotated', style({ transform: 'rotate(-180deg)' })),
              transition('rotated => default', animate('400ms ease-out')),
              transition('default => rotated', animate('400ms ease-in'))
        ]);
    ])
    ],
...

export class RotateComponent { 

    state: string = 'default';

    rotate() {
        this.state = (this.state === 'default' ? 'rotated' : 'default');
    }

}
Run Code Online (Sandbox Code Playgroud)

在模板中:

<button (click)="rotate()">Press me to rotate</button>
Run Code Online (Sandbox Code Playgroud)

并确保添加绑定到标记:

[@rotatedState]='state'
Run Code Online (Sandbox Code Playgroud)

此外,请确保将动画模块导入到您的应用中,如下所示:

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

@NgModule({
  ...,
  imports: [
    ...,
    BrowserAnimationsModule
  ],
  ...
})
Run Code Online (Sandbox Code Playgroud)

  • 只需将 rotate(-180deg) 更改为 rotate(-360deg) 。这将进行完整的旋转。如果这个答案对你有帮助,请点赞 :) (2认同)