:enter和:leave角动画没有平滑过渡

问题描述 投票:0回答:1

我想使用Angular动画流畅地滑入和滑出动画。我正在使用ngSwitch显示/隐藏这些组件。传入的组件将前一个组件向上推,然后消失。我尝试添加延迟,但这不能解决问题。

app.component.html

<div class="container" [ngSwitch]="counter">
  <div @slideInOut *ngSwitchCase="1"></div>
  <div @slideInOut *ngSwitchCase="2"></div>
  <div @slideInOut *ngSwitchCase="3"></div>
  <div @slideInOut *ngSwitchCase="4"></div>
</div>

<button (click)="handleNext()">Next</button>

app.component.scss

.container {
  div {
    width: 100px;
    height: 100px;
    background: pink;
  }
}

app.component.ts

import { Component } from '@angular/core';
import { slideInOut } from './shared/animations';


@Component({
  selector: 'my-app',
  styleUrls: ['./app.component.scss'],
  animations: [ slideInOut ],
  templateUrl: './app.component.html',
})
export class AppComponent {
  readonly name: string = 'Angular';
  counter = 1;

  boxArr = [1, 2, 3, 4]

  handleNext(){
    if(this.counter <= this.boxArr.length){
      this.counter += 1
    } else {
      this.counter = 1
    }
  }
}

animations.ts

import { trigger, state, style, transition, animate, keyframes } from "@angular/animations";


export const slideInOut =  trigger('slideInOut', [
    transition(':enter', [
      style({transform: 'translateX(100%)'}),
      animate('200ms ease-in', style({transform: 'translateX(0%)'}))
    ]),
    transition(':leave', [
      animate('200ms ease-in', style({transform: 'translateX(-100%)'}))
    ])
])

演示:https://stackblitz.com/edit/angular-na1ath

javascript angular angular-animations
1个回答
0
投票

问题是:

1)当您处理display: block时,元素将占据全部行宽,无论是否有剩余空间,因此幻灯片都位于两行中

2)如果我们通过添加display: inline-block来解决此问题,则必须解决的是,当有两个元素时,它们比一个元素占用更多的空间。同时,translate实际上不会移动元素,而只会移动它的可见部分(“物理”形状处于相同位置)

因此,通过绝对定位幻灯片来解决

https://stackblitz.com/edit/angular-zciezz?file=src/app/app.component.scss

.container {
  position: relative;
  width: 100px;
  height: 100px;

  div {
    position: absolute;
    top: 0;
    left: 0;
    width: 100px;
    height: 100px;
    background: pink;
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.