首頁  >  文章  >  web前端  >  以下是幾種可能的標題: 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

Linda Hamilton
Linda Hamilton原創
2024-10-26 22:14:29766瀏覽

以下是几种可能的标题:

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 從右側滑入?

<code class="html"><div class="note" [ngClass]="{'transition':show}" *ngIf="show">
  <p> Notes</p>
</div>
<button class="btn btn-default" (click)="toggle(show)">Toggle</button></code>
.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 上的動畫

<code class="typescript">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: `
    <button (click)="show = !show">toggle show ({{show}})</button>

    <div *ngIf="show" [@enterAnimation]>xxx</div>
  `
})
export class App {
  show:boolean = false;
}</code>

原始回答🎜>

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

可以使用 hidden 屬性來取代:

<code class="html"><div class="note" [ngClass]="{'transition':show}" [hidden]="!show"></code>

以上是以下是幾種可能的標題: 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的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn