【问题标题】:Angular animation : Rotation 180° click image角度动画:旋转 180° 点击图片
【发布时间】:2022-03-16 19:39:56
【问题描述】:

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

我也使用 Angular 材质。

这是我的按钮:

 <!-- 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>

此按钮将打开一个侧导航,我想对其进行动画处理以使其更加用户友好

谢谢

【问题讨论】:

    标签: html angular angular-animations


    【解决方案1】:

    您不需要材料,因为 Angular 在其核心模块中引入了内置动画。举个例子:

    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');
        }
    
    }
        
    

    在模板中:

    <button (click)="rotate()">Press me to rotate</button>
    

    并确保为您正在操作的标签添加绑定:

    <img [@rotatedState]='state' />
    

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

    import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
    
    @NgModule({
      ...,
      imports: [
        ...,
        BrowserAnimationsModule
      ],
      ...
    })
    

    使用工作示例检查stackblitz

    【讨论】:

    • 是否可以将图像旋转 4 面?你能告诉我怎么做吗?
    • 你的意思是做一个完整的旋转,以便图像在起始位置结束?
    • 只需将 rotate(-180deg) 更改为 rotate(-360deg) 。这将做一个完整的旋转。如果这个答案对你有帮助,请点赞:)
    • 感谢您的回答,但用户可以旋转我们需要大约 4 个状态(90,180,270,360 度)的不稳定图像,类似于 *.com/questions/16794310/…
    • 如何保存旋转后的图片?
    最近更新 更多