」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 以下是幾種可能的標題: 1. How to Make CSS Transitions Work with `ngIf` in Angular 2? 2. Why Does `ngIf` Break My CSS Transitions in Angular 2? 3. Angular 2: Combining `ngIf` and CSS Animations for Smooth Trans

以下是幾種可能的標題: 1. How to Make CSS Transitions Work with `ngIf` in Angular 2? 2. Why Does `ngIf` Break My CSS Transitions in Angular 2? 3. Angular 2: Combining `ngIf` and CSS Animations for Smooth Trans

發佈於2024-11-08
瀏覽:948

以下是几种可能的标题:

1. How to Make CSS Transitions Work with `ngIf` in Angular 2? 
2. Why Does `ngIf` Break My CSS Transitions in Angular 2?
3. Angular 2: Combining `ngIf` and CSS Animations for Smooth Transitions
4. Troubleshooting CSS Transitions and `ngIf` in

Angular 2 的 ngIf 和 CSS 過渡/動畫

如何在 Angular 2 中使用 CSS 將 div 從右側滑入?

Notes

.transition{
  -webkit-transition: opacity 1000ms ease-in-out,margin-left 500ms ease-in-out;
  -moz-transition: opacity 1000ms ease-in-out,margin-left 500ms ease-in-out;
  -ms-transition: opacity 1000ms ease-in-out,margin-left 500ms ease-in-out ;
  -o-transition: opacity 1000ms ease-in-out,margin-left 500ms ease-in-out;
  transition: opacity 1000ms ease-in-out,margin-left 500ms ease-in-out;
  margin-left: 1500px;
  width: 200px;
  opacity: 0;
}

.transition{
  opacity: 100;
  margin-left: 0;
}

如果僅使用 [ngClass] 來切換類,並且 利用 opacity,則該程式碼可以正常運作。但不想讓該元素從一開始就被渲染,所以先用 ngIf "隱藏"它,但此時過渡將不起作用。

更新 4.1.0

使用了過渡動畫API,無需再利用 [hidden] 或 [*ngIf hidden]。

更新2.1.0

查看angular.io 上的動畫

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

@Component({
  selector: 'my-app',
  animations: [
    trigger(
      'enterAnimation', [
        transition(':enter', [
          style({transform: 'translateX(100%)', opacity: 0}),
          animate('500ms', style({transform: 'translateX(0)', opacity: 1}))
        ]),
        transition(':leave', [
          style({transform: 'translateX(0)', opacity: 1}),
          animate('500ms', style({transform: 'translateX(100%)', opacity: 0}))
        ])
      ]
    )
  ],
  template: `
    

    
xxx
` }) export class App { show:boolean = false; }

原始回答

當表達式變為 false 時,*ngIf 會從 DOM 中移除元素,一個不存在的元素是無法進行過渡的。

可以使用hidden 屬性來取代:

最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3