我想使用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%)'}))
])
])
答案 0 :(得分:1)
问题在于:
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;
}
}