我正在try 添加一个动画到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()] });

stackblitz


参考文献

  1. JSfiddle demo
  2. article

Css相关问答推荐

如何改变图标的 colored颜色 时悬停在

如何在移动屏幕尺寸上包装元素?

悬停时可展开的按钮与屏幕右侧对齐,如何保持未悬停的按钮正确对齐?

无限交替css动画之间的未知延迟

我无法在css中设置填充或边距

CSS 内联 Flex 和段落换行

Angular Material - 将 matsnackbar 置于所有对话框之上

动态覆盖 Vuetify 类

CSS 网格 - 具有独立可滚动区域的两列布局

在变量中存储匹配 Select 器的子元素的计数

根据选项 colored颜色 更改 Select 文本 colored颜色

Tailwind css 和 Next.js 导航组件 z-index 不工作

问题定位位置:Tailwindcss 中的绝对元素

如何为以下布局安排网格?

如何制作一个div来包装两个浮动div?

弹性盒项目之间的间距

删除所有填充和边距表格 HTML 和 CSS

CSS图像最大宽度设置为原始图像大小?

:last-child 没有按预期工作?

Rails 中正确的 SCSS assets资源 struct