角度动画在子div上留下过渡

时间:2017-07-13 15:49:21

标签: angular angular-animations

输入动画工作正常,但离开动画无效。 如果我将@modalFadeZoom移动到父级,则两个转换都有效,但问题是缩放不是从模态的中心发生的,这会产生奇怪的动画。

如果我将@modalFadeZoom移动到子节点,则淡入淡出可以正常工作。 但没有淡出并放大关闭模型。

组件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档

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

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

plnkr链接

请注意我已在plnkr演示中将动画移动到父级 由于缩放,动画从中心开始。

https://plnkr.co/Ldn4wJwuZMaaunVWuYpx

1 个答案:

答案 0 :(得分:3)

将动画声明更改为:

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)' })),
      ]
    ),
  ])

将动画移动到最外面的元素(model-dialog)。

当他们是* ngIf中的孩子时,Angular不会播放动画。