4

I have added Enter and Leave animation in my Angular component. Where I am animating the height of a div based on a boolean variable. The animation is working fine but Is there a way to turn off animation on the initial view load?

Component.ts

    animations: [
            trigger('slideInOut', [
                transition(':enter', [
                    style({ height: '0', overflow: 'hidden', opacity: 0 }),
                    animate('200ms ease-in-out', style({ height: '*', opacity: 1 }))
                ]),
                transition(':leave', [
                    animate(
                        '200ms ease-in-out',
                        style({ height: 0, overflow: 'hidden', opacity: 0 })
                    )
                ])
            ])
    ]

Component.html

<div class="sidebar-sub-menu" *ngIf="isSubMenuOpen || setSubmenuActive" [@slideInOut]>
  ...
</div>
1

1 Answer 1

5

Use [@.disabled]="isPaused" on the element itself to prevent animation; where isPaused is a boolean variable that is equal to true.

In your case, you could initialize the isPaused variable to true in the constructor, and then it's your choice, either re-enable it using timeout like this

constructor() {
    this.isPaused= true;
    setTimeout(() => {
      this.isPaused= false;
    }, 1000);
}

Or you could assign it to true on a specific condition.

Hope this helped.

Sign up to request clarification or add additional context in comments.

1 Comment

It worked, I have enabled the animation in afterviewinit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.