代码之家  ›  专栏  ›  技术社区  ›  Blake

无法使用CSS旋转Angular 15中的字体图标

  •  -1
  • Blake  · 技术社区  · 8 月前

    我正试图让一个带有fa图标的按钮旋转180度,然后在再次单击时反向旋转。到目前为止,我已经有了这个(在stackblitz中复制),但当点击按钮时,图标根本不会旋转。这可以通过CSS实现吗?还是我需要换一种方式?

    烟囱闪电战: https://stackblitz.com/edit/angular-ivy-5nbjjw?file=src%2Fapp%2Fapp.component.css

    app.component.css

    .expandButton:focus i {
      animation: rotate 1s ease-in-out 0s;
    }
    
    @keyframes rotate {
      from {
        transform: rotate(0deg);
      }
      to {
        transform: rotate(360deg);
      }
    }
    

    app.component.ts

    import { Component, VERSION } from '@angular/core';
    import { faChevronDown } from '@fortawesome/free-solid-svg-icons';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css'],
    })
    export class AppComponent {
      faChevronDown = faChevronDown;
    }
    

    app.component.html

    <button class="expandButton" mat-raised-button color="primary" type="button">
      <i><fa-icon [icon]="faChevronDown"></fa-icon></i>
    </button>
    

    app.module.ts

    import { NgModule } from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    import { FormsModule } from '@angular/forms';
    
    import { AppComponent } from './app.component';
    import { PhotoGalleryComponent } from './components/photo-gallery/photo-gallery.component';
    import {
      FaIconLibrary,
      FontAwesomeModule,
    } from '@fortawesome/angular-fontawesome';
    import { faChevronDown } from '@fortawesome/free-solid-svg-icons';
    
    @NgModule({
      imports: [
        BrowserModule,
        FormsModule,
        PhotoGalleryComponent,
        FontAwesomeModule,
      ],
      declarations: [AppComponent],
      bootstrap: [AppComponent],
    })
    export class AppModule {
      constructor(library: FaIconLibrary) {
        library.addIcons(faChevronDown);
      }
    }
    
    0 回复  |  直到 8 月前
        1
  •  2
  •   Henrik Bøgelund Lavstsen    8 月前

    只需制作 i display: block

    所以你想要在css中得到的结果

    .expandButton:focus i {
      display: block;
      animation: rotate 1s ease-in-out 0s;
    }
    
        2
  •  2
  •   HenryDev    8 月前

    如果你已经使用了很棒的字体,我不会强制使用CSS进行旋转。您可以简单地使用属性绑定来获得更干净的解决方案。

    <i><fa-icon [icon]="!slideOpen ? faChevronDown : faChevronUp"></fa-icon></i>
    

    这是一个作品 SOLUTION

    希望它能有所帮助!