Angular animation leave transition on child div

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 06:36:13

问题


Enter animation is working fine, but the leave animation is not working. if I move the @modalFadeZoom to parent both transitions works, but the problem with that is the scaling is not happening from center of the modal, which is giving weird animation.

If i move @modalFadeZoom to the child the fade in zoom out is working fine. but no fade out and zoom in on closing the model.

component html

<div class="modal-dialog" *ngIf="showModal">
        <div class="modal-content" [@modalFadeZoom]>
            <div class="modal-header">
                <h5 class="modal-title">SOME TITLE</h5>
                <button type="button" (click)="showModal=false" class="close" aria-label="Close">
          <span aria-hidden="true">×</span>
        </button>
            </div>
            <div class="modal-body">
                lorem ipsum etc etc
            </div>
        </div>
    </div>
    <div class="modal-backdrop fade show" *ngIf="showModal"></div>

ts file

import { Component, OnInit, trigger, transition, style, animate } from '@angular/core';

    @Component({
        templateUrl: 'modal.template.html',
        animations: [
            trigger(
                'modalFadeZoom',
                [
                    transition(
                        ':enter', [
                            style({ transform: 'scale(.7)', opacity: 0 }),
                            animate('0.3s', style({ opacity: 1, transform: 'scale(1)' })),
                        ]
                    ),
                    transition(
                        ':leave', [
                            style({ opacity: 1, transform: 'scale(1)' }),
                            animate('5.3s', style({ opacity: 0, transform: 'scale(.7)' })),
                        ]
                    ),
                ])
        ]
    })

    export class ModalComponent implements OnInit {

        private showModal = false;;

        ngOnInit(): void {
            this.showModal = true;
        }
    }

css for modal-dialog

.modal-dialog {
    position: fixed;
    top: 50%;
    left: 50%;
    height: auto;
    z-index: 2000;
    transform: translateX(-50%) translateY(-50%);
}

plnkr link

notice that i have moved animation to parent in the plnkr demo animation starts off from the center, because of scaling.

https://plnkr.co/Ldn4wJwuZMaaunVWuYpx


回答1:


Change your animation declaration to this:

trigger(
  'modalFadeZoom',
  [
    transition(
      ':enter', [
        style({ transform: 'translateX(-50%) translateY(-50%) scale(.7)', opacity: 0 }),
        animate('0.3s', style({ opacity: 1, transform: 'translateX(-50%) translateY(-50%) scale(1)' })),
      ]
    ),
    transition(
      ':leave', [
        style({ opacity: 1, transform: 'translateX(-50%) translateY(-50%) scale(1)' }),
        animate('5.3s', style({ opacity: 0, transform: 'translateX(-50%) translateY(-50%) scale(.7)' })),
      ]
    ),
  ])

And move the animation to the outermost element(model-dialog).

Angular doesn't play leaving animations when they're children inside an *ngIf.



来源:https://stackoverflow.com/questions/45085604/angular-animation-leave-transition-on-child-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!