Implement an overflow property at the end of an Angular 2 animation

To create a smooth slide in and out animation, I have implemented the following code:

animations: [
        trigger('assignState', [
            state('maximize', style({
                height: '*',
            })),
            state('minimize',   style({
                height: '0'
            })),
            transition('maximize => minimize', animate('300ms ease-in')),
            transition('minimize => maximize', animate('300ms ease-out'))
        ])
    ]

In order for this animation to function correctly, I found that adding overflow: hidden to the element is necessary. However, having overflow: hidden when the element is maximized causes issues with the content display. Some absolute elements within it are not visible due to the overflow hidden property.

Is there a way to automatically apply overflow hidden when the minimize state animation begins and remove it once the maximize animation has finished?

Thank you

Answer №1

To completely control this situation, you can manage it within the animation definition by:

1) Setting the desired overflow configurations in your states:

state(
  'maximize', 
  style({
    height: '*',
    overflow: 'visible' // or any other value like 'inherit'
})),
state('minimize',
  style({
    height: '0',
    overflow: 'hidden'
}))

Next, utilize group to establish different timing functions for height and overflow. This allows you to employ the steps() timing function on the overflow property, creating a timed toggle effect:

transition(
  'maximize => minimize',
  group([
    animate('300ms ease-in', style({ height: 0 })),
    animate('300ms steps(1,start)', style({ overflow: 'hidden' }))
  ])
),
transition(
  'minimize=> maximize',
  group([
    animate('300ms ease-out', style({ height: '*' })),
    animate('300ms steps(1,end)', style({ overflow: 'visible' }))
  ]) 
)

Note that specifying end and start on the steps timing function determines when the target style is applied for each step. For instance, in the 'maximize=> minimize' transition, you want to hide the overflow as soon as the animation begins.

An alternative syntax using the angular animation keyframes format can also be employed to define the steps. For example, the 'maximize => minimize' transition could look like this:

transition(
  'maximize => minimize',
  group([
    animate('300ms ease-in', style({ height: 0 })),
    animate(
      '300ms', 
      keyframes([
        style({overflow: hidden}),
        style({overflow: hidden})
      ])  
    )
  ])
)

It's worth mentioning that styles defined in the keyframe function vanish once the animation concludes, but since overflow is set within the states, they will remain as intended after the animation finishes.

Answer №2

Implementing the start and end callback functions with angular2-animation is essential.

The code snippet below serves as a reference example:

<ul>
    <li *ngFor="let hero of heroes"
        (@flyInOut.start)="animationStarted($event)"
        (@flyInOut.done)="animationDone($event)"
        [@flyInOut]="'in'">
      {{hero.name}}
    </li>
  </ul>

For more detailed information on these animation callbacks, visit: https://angular.io/docs/ts/latest/guide/animations.html#!#animation-callbacks

UPDATE: Another option for your scenario is utilizing the keyframe feature.

Learn more about multi-step animations with keyframes here: https://angular.io/docs/ts/latest/guide/animations.html#!#multi-step-animations-with-keyframes

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

What could be the reason for the text covering the image on the larger breakpoint?

I am having an issue with this code where the Lorem Ipsum text is not staying centered at all times. Instead, it overlaps the image when I reach the large breakpoint. Can you please explain why this is happening and suggest a solution? Here is the curren ...

Is there a way to align this button vertically to the bottom of its container with Bootstrap 5?

I'm currently working from this codepen snippet and facing an issue where I can't seem to align the button in the left box to the bottom of the box. Despite trying multiple approaches, I haven't been able to achieve the desired alignment. C ...

Exploring the Comparison Between Angular RxJS Observables: Using takeUntil and unsubscribing via Subscription

When it comes to unsubscribing from observables in Angular components utilizing ngOnDestroy, there are multiple approaches available. Which of the following options is more preferable and why? Option 1: takeUntil The usage of RxJS takeUntil for unsubscri ...

Issue: The token is required in Angular 2 RC5 to proceed with the testing

In June 2016, an article was written on testing Angular 2 applications using angular2-seed as the starting point. For a tutorial rewrite using Angular CLI (master branch) with Angular 2 RC5, a strange error is encountered during testing: Error: Token mus ...

Using CSS to create a silhouette of a PNG image

Does anyone have any tips on how to use CSS to turn a PNG image with transparency into a black silhouette? For example, from this: To this: I have multiple images to do this for which is why I prefer not to use Photoshop. ...

Styling with CSS3: positioning floating divs on top of a non-floated div

Checkout this fiddle here: http://jsfiddle.net/PA5T5/1/ I've got a setup with 3 divs - 2 are floated whereas one is not. The two floating divs are positioned on top of the non-floating div. I'm wondering if this approach is correct or if it&apos ...

Error in Angular2: The provided parameters do not match any of the available function signatures

I have implemented logic in the code snippet below to dynamically adjust rows and columns based on my specific business requirements. However, when I include this code in my Angular2 TypeScript file, I encounter an error stating that the supplied paramet ...

Implementing nested CSS styles with jQuery - a step-by-step guide!

In my stylesheet.css file, I have the following code: .vertical-navigation .navbar-default .navbar-nav > li > a:hover { background-image: url(../images/sticcky_nav_hover.png) } .vertical-navigation .navbar-default .navbar-nav > li > a.navf ...

Is there a way to target a child element for hover effect in CSS without affecting the parent element?

Is it feasible to hover over a nested child element without activating a hover effect on the parent element? In the demonstration below, you'll notice that even when hovering over the child, the hover effect on the parent is still in place. I am int ...

Anticipated request for spy navigation with path '/members' was expected, but unfortunately was not triggered

I am facing an issue with a service method that performs an HTTP delete operation. The expected behavior is that upon successful deletion, the page should be redirected to another location. However, during testing, I noticed that the router navigation func ...

Align the text vertically in a Bootstrap button

Can anyone lend a hand with vertically aligning text in a Bootstrap button? When I use the align-middle class, the text is aligned as desired. However, if I use the align-top class, the text remains top-aligned no matter what I do. Any suggestions? Here& ...

Leveraging the p-steps module from PrimeNG for smaller screen sizes

I am facing an issue with resizing the p-steps component from PrimeNG to avoid overflow in smaller screen resolutions. The current code I am using is as follows: <div class="d-flex card mb-4"> <p-steps [model]="items" ...

Click event for jQuery horizontal accordion

I'm attempting to create a simple horizontal accordion-style element. My setup includes three 'banner' divs and three 'area' divs. Ideally, when I click on a banner, the corresponding area should animate - expanding in width to au ...

Incorporating Angular module into the mean.io bundle

Need help with adding the angular-ui-grid module to a mean.io package: $ cd packages/custom/mypackage $ npm install angular-ui-grid --save To import the JS, add this line to packages/custom/mypackage/public/index.js: import 'angular-ui-grid'; ...

Unveil concealed information within a freshly enlarged container

As I organize my content into an FAQ format, I want users to be able to click on a link and expand the section to reveal a list of items that can also be expanded individually. My goal is to have certain list items expand automatically when the FAQ section ...

The CORS policy is blocking access to the site from NodeJS, Firebase Functions, and Angular because the 'Access-Control-Allow-Origin' header is not present

I've encountered a CORS error while hosting my Angular frontend and Node backend on Firebase Hosting and Firebase Functions. Despite trying various solutions from Stackoverflow and other sources, the issue persists for POST methods. Below, I've s ...

Solve the error "Property 'container' of null is not accessible" in musickit.js while running an Angular application on a server

I am currently developing an application that combines Angular and MusicKit to offer users the ability to listen to music simultaneously. However, I encountered a challenging error when trying to run the application using ng serve --host x.x.x.x instead of ...

What is causing this error to appear in Next.js? The <link rel=preload> is showing an invalid value for `imagesrcset`

I've got a carousel displaying images: <Image src={`http://ticket-t01.s3.eu-central-1.amazonaws.com/${props[organizationId].events[programId].imgId}_0.cover.jpg`} className={styles.carouselImage} layout="responsive" width={865} ...

sophisticated HTML navigation bar

I am looking for a way to create a menu where the drop-down items overlay the rest of the menu when the screen width is small. Here is my current code: nav{ line-height:100%; opacity:.9; }nav ul{ background: #999; padding: 0px; border-radius: 20px; ...

Is it advisable to specify data types for my JSON data in TypeScript?

For the shopping application in my project, I am utilizing a JSON structure to categorize products as either hot or branded. However, I encountered an issue when trying to define a type for the entire JSON object labeled "full". Despite my attempts, it app ...