我正在尝试向 Angular 17 应用程序添加动画,但它没有按预期工作。
基本上有两个
divs
,并排,按下按钮时左侧被 *ngIf
隐藏。左侧的动画按预期工作,但右侧 div
不跟随,仅在动画完成时调整大小。
这里是示例代码:https://stackblitz-starters-uwgcaa.stackblitz.io
如何使动画在两侧都起作用?
提前致谢。
请将动画从
translateX
更改为margin-left
,查阅CSS教程找到了解决方案!
import {
animate,
group,
query,
style,
transition,
trigger,
} from '@angular/animations';
import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import {
BrowserAnimationsModule,
provideAnimations,
} from '@angular/platform-browser/animations';
import 'zone.js';
@Component({
selector: 'app-root',
standalone: true,
template: `
<div class="container">
<div class="menu" *ngIf="showMenu" [@slideInOut]="showMenu">Menu</div>
<div class="content">Content<button (click)="toggleMenu()">Teste</button></div>
</div>
`,
animations: [
trigger('slideInOut', [
transition(':enter', [
style({ marginLeft: '-100%' }),
animate('200ms', style({ marginLeft: '0px' })),
]),
transition(':leave', [
animate('200ms', style({ marginLeft: '-100%' })),
]),
]),
],
imports: [CommonModule],
})
export class App {
name = 'Angular';
showMenu = true;
toggleMenu() {
this.showMenu = !this.showMenu;
}
}
bootstrapApplication(App, { providers: [provideAnimations()] });