我刚刚创建了我的第一个Angular 项目,并试图使用Angular 动画来动画我的内容滚动(我希望内容在我滚动页面时出现),并添加一个令人震惊的动画.

由于某些原因,它不适合我,并考虑到角17是新的,并带来了许多变化,我找不到足够的资源来举例.

HTML文件组件:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    @defer (on viewport;){
    <div class="grid grid-items" [@staggerAnimation]="projects.length">
        @for (project of projects; track project.id;) {
        <div class="container">
            <h4>{{project.name}}</h4>
            <p>
                {{project.description}}
            </p>
            <a class="explore-btn" href="{{project.link}}" target="_blank">Explore</a>
        </div>
        }
    </div>
    } @placeholder {
    <div>Loading Projects...</div>
    }
</body>

</html>

TS文件组件:

import { Component } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import 'zone.js';
import { provideAnimations } from '@angular/platform-browser/animations';
import {
  trigger,
  transition,
  query,
  stagger,
  animate,
  keyframes,
  style,
} from '@angular/animations';

@Component({
  selector: 'app-project-card',
  standalone: true,
  imports: [],
  templateUrl: './project-card.component.html',
  styleUrl: './project-card.component.css',
  providers: [],
  animations: [ 
    trigger('staggerAnimation', [
      transition('* => *', [
        query(':enter', style({opacity: 0}), {optional: true}),
        query(':enter', stagger('300ms', [
          animate('1s ease-in', keyframes([
            style({opacity: 0, transform: 'translateY(-75px)', offset: 0}),
            style({opacity: 0.5, transform: 'translateY(35px)', offset: 0.3}),
            style({opacity: 1, transform: 'translateY(0)', offset: 1}),
          ]))
        ]))
      ])
    ])
  ],
})
export class ProjectCardComponent {

  projects = [ PROJECT OBJECTS HERE]

bootstrapApplication(ProjectCardComponent, {
  providers: [provideAnimations()],
});

*我使用的是Angular 17的独立项目,因此不需要NgModule文件

I tried placing the trigger at the div with "container" and still didn't work.
I also tried removing the @defer which still lead to nothing.

不确定它是否相关,但此组件是子组件.

推荐答案

第一个问题是动画没有被添加到html中,它是用[@staggerAnimation]="projects.length"来完成的

在此之后,请将延迟向上移动到主div,这会导致动画无法工作,请在下面找到一个工作示例!

import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { bootstrapApplication } from '@angular/platform-browser';
import 'zone.js';
import { provideAnimations } from '@angular/platform-browser/animations';
import {
  trigger,
  transition,
  query,
  stagger,
  animate,
  keyframes,
  style,
} from '@angular/animations';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [CommonModule],
  providers: [],
  template: `
  @defer (on viewport;){
    <div class="grid grid-items"  [@staggerAnimation]="projects.length">
        @for (project of projects; track project.id;) {
        <div class="container">
            <h4>{{project.name}}</h4>
            <p>
                {{project.description}}
            </p>
            <a class="explore-btn" href="{{project.link}}" target="_blank">Explore</a>
        </div>
        }
    </div>
        } @placeholder {
        <div>Loading Projects...</div>
        }
    <button (click)="add()">add</button>
  `,
  animations: [
    trigger('staggerAnimation', [
      transition('* => *', [
        query(':enter', style({ opacity: 0 }), { optional: true }),
        query(
          ':enter',
          stagger('300ms', [
            animate(
              '1s ease-in',
              keyframes([
                style({
                  opacity: 0,
                  transform: 'translateY(-75px)',
                  offset: 0,
                }),
                style({
                  opacity: 0.5,
                  transform: 'translateY(35px)',
                  offset: 0.3,
                }),
                style({ opacity: 1, transform: 'translateY(0)', offset: 1 }),
              ])
            ),
          ])
        ),
      ]),
    ]),
  ],
})
export class App {
  name = 'Angular';
  id = 5;
  projects = [
    { name: 'test', description: 'test', link: 'https://google.com', id: 1 },
    { name: 'test', description: 'test', link: 'https://google.com', id: 2 },
    { name: 'test', description: 'test', link: 'https://google.com', id: 3 },
    { name: 'test', description: 'test', link: 'https://google.com', id: 4 },
    { name: 'test', description: 'test', link: 'https://google.com', id: 5 },
  ];

  add() {
    this.id = this.id++;
    this.projects.push({
      name: 'test',
      description: 'test',
      link: 'https://google.com',
      id: this.id,
    });
  }
}

bootstrapApplication(App, {
  providers: [provideAnimations()],
});

stackblitz

Html相关问答推荐

如何防止SVG图标出现断行?

将CSS应用于TD标记内的div

只有一行上有溢出的CSS网格

将弹性容器设置为内容宽度

react :事件和转发器在特定代码段中不起作用

为什么在添加文本时网格区域会调整大小?

在css中,如何在英雄版块的标题后面做一个粘性的导航栏?

你将如何为手机制作导航栏

如何将文本环绕心形(而不仅仅是圆形)?

柔性盒内的物品;t覆盖整个宽度,即使设置为100%

悬停表格单元格文本时,使元素的字体大小更大,同时保持表格单元格的高度不变

顶部有文字的图像的悬停效果

并排对齐两个文本区域列

当作为常规图片输入时,Cloudfront分布式图像可显示,但作为背景图像不显示

主要元素内滚动时,固定导航栏会消失