Angular2 Cascading Animations

I have been working on implementing cascaded animations for a list of elements.

Although I successfully applied the state triggers, following the guidelines in the documentation, I am encountering an issue where all element states are being applied simultaneously rather than cascading as desired.

This is what my animation currently looks like:

https://i.sstatic.net/wwTKG.gif

The expected outcome should be something along these lines:

https://i.sstatic.net/t46FY.gif

heroes.component.ts

trigger('flyInOut', [
  state('in', style({
    transform: 'translateX(0)'
  })),
  transition('void => in', [
    style({transform: 'translateX(-100px)'}),
    animate(500)
  ]),
  transition('* => void', [
    style({transform: 'translateX(0)'}),
    animate(500)
  ])
])

heroes.component.html

<li *ngFor="let hero of heroes" 
    (click)="onSelect(hero)"
    [class.selected]="hero === selectedHero"
    @heroState="hero === selectedHero ? 'active' : 'inactive'"
    @flyInOut="'in'"
    >
  <span class="badge">{{hero.id}}</span> {{hero.name}}
  <button class="delete" (click)="delete(hero); $event.stopPropagation()">
    x
  </button>
</li>

Answer №1

If you're looking to achieve a specific animation effect, then you'll want to utilize the stagger feature in Angular.

The concept is to apply an animation to the container of your list (perhaps an <ol> element) based on changes in the length of the list or any other dynamic factor. By identifying entering and leaving elements within the list, you can use the stagger function to create a visually pleasing cascading effect.

Check out the provided link for detailed documentation, and consider customizing the example code snippet below to tailor the animation to your specific needs:

transition('* => *', [
  query(':leave', [
    stagger(100, [
      animate('0.5s', style({ left: 100 }))
    ])
  ]),
  query(':enter', [
    style({ left: -100 }),
    stagger(100, [
      animate('0.5s', style({ left: 0 }))
    ])
  ])
])

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide for converting a JavaScript function with spread arguments of different types to C# style

I am having difficulty with the strict typing in C# when it comes to function arguments. For my Reverse Polish Notation (RPN) calculator, the required arguments will be passed through a function call using a comma-separated list of different types: this.F ...

Angular: seamlessly transferring all directives from parent component to child without interference

Imagine we have a component called main that includes another one named sub. I would like for the directive passed to main in the client side, such as: <main dir1='dirval1'></main> for the main component to act as a thin wrapper and ...

Developing personalized middleware definition in TypeScript for Express

I have been struggling to define custom middleware for our application. I am using [email protected] and [email protected]. Most examples of typing middleware do not involve adding anything to the req or res arguments, but in our case, we need to modify ...

angular component that takes a parameter with a function

Is there a way for my angular2 component to accept a function as a parameter? Uncaught Error: Template parse errors: Can't bind to 'click' since it isn't a known property of 'input'. (" minlength="{{minlength}}" [ERROR -&g ...

Typescript: Implementing a generic function with the flexibility of an optional parameter

Having some difficulty writing a generic function with an optional parameter type Action<TParameters = undefined> = (parameters: TParameters) => void const A: Action = () => console.log('Hi :)') // This works as expected const B: ...

How can I define Record values in Typescript based on their specific keys?

I am working on creating a custom data structure that allows me to store values with string keys within the union string | number | boolean: type FilterKey = string; type FilterValue = string | number | boolean; type Filters<K extends FilterKey, T exten ...

Retrieve information from a Firestore document using the document's unique identifier

Running into some issues with my Angular 6 and Ionic 4 app while trying to retrieve specific data from Google Cloud Firestore. I've set up "cards" to display various stats retrieved from the Firestore database. You can see an example of how my cards a ...

Using Typescript for Asynchronous Https Requests

I've been attempting all day to make an https request work. My current code isn't functioning as expected; when I run it, I encounter an "Unhandled error RangeError: Maximum call stack size exceeded at Function.entries" import * as https from &q ...

Efficient Searching with Typescript and Lodash: Boosting Performance with Arrays and Objects

I am faced with the challenge of converting between two classes called MyObject and MyObjectJSON, which have helper methods to assist in the conversion process: myObj.toJSON() and MyObject.fromJSON(). Currently, I have instances of these classes represent ...

Embedding a table row within an anchor element in Angular 4

Currently, I am facing an issue with a table that contains [routerLink] on each <tr>. This setup is preventing the "open link in a new tab" option from appearing when I right-click because there is no <a> tag. I attempted to enclose the table r ...

Collection of personalized forms where the parent is a FormGroup

One scenario I'm working on involves creating multiple custom formgroup classes that have FormGroup as their parent class, structured like this: export class CustomFormGroup1 extends FormGroup { //custom properties for this FormGroup const ...

Using the as operator in TypeScript for type casting a string

function doSomething(a : any) { let b = (a as Array<any>) alert(typeof b) // displays "string" } doSomething("Hello") The alert is showing "string" instead of what I anticipated, which was something along the lines of a null value. The docu ...

What is the process for transferring files from a NodeJS website to Azure CDN?

I am searching for a solution to upload images directly to Azure CDN. Here is the situation: My client Portal built with Angular (4.x) enables users to manage their website, and they need the capability to upload images that will be displayed on the sit ...

Ways to transfer a value between two different card elements

I have designed a component with three div cards side by side using bootstrap. The first card displays a list of products, and my intention is that when a product is clicked, the details should appear in the second card on the same page. However, this fun ...

Leveraging Java and TypeScript code for specific functionalities within an Ionic 2 Android application

When it comes to creating hybrid apps that support Windows, iOS, and Android simultaneously using web technologies such as Html, CSS, and Js, Ionic is the go-to choice. However, there may be certain features not supported by the Ionic 2 framework. Is it ...

What is the best way to retrieve parameters from a URL in Angular and Express?

My URL appears as http://www.example.com/idf34he8sf/9iad2hf7usnf. I am trying to extract the parameters idf34he8sf and 9iad2hf7usnf This is how I have approached it: In Angular this.route.paramMap.subscribe(params => { this.organizationId = par ...

Steps for making a GET request from an API in Angular 7

Currently, I am attempting to retrieve JSON data using HttpClient in Angular 7. The code is functioning properly, but I am exploring the option of fetching the data directly from the API URL instead of relying on the const IMAGES array. import { Injectable ...

Could you provide insight into the reason behind debounce being used for this specific binding?

function debounce(fn, delay) { var timer return function () { var context = this var args = arguments clearTimeout(timer) timer = setTimeout(function () { fn.apply(context, args) }, delay) ...

"Encountered a problem with Next JS API while trying to fetch data from the app directory

import { NextResponse } from "next/server"; export async function POST(request: Request) { const data = await request.json(); console.log(data); return NextResponse.json({ foo: "boo" }); } next version = "next": &quo ...

Is the Inline Partial<T> object still throwing errors about a missing field?

I recently updated to TypeScript version ~3.1.6 and defined an interface called Shop as follows: export interface Shop { readonly displayName: string; name: string; city: string; } In this interface, the property displayName is set by the backend a ...