【问题标题】:Angular animation leave transition on child div子div上的角度动画离开过渡
【发布时间】:2017-12-18 12:43:08
【问题描述】:

进入动画工作正常,但离开动画不工作。 如果我将@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

【问题讨论】:

  • @Vega 我也用 css 更新了问题
  • 无论出于何种原因,Angular 都不会为 *ngIf 模板中的子元素留下动画。尝试将动画移动到具有 *ngIf 模板的同一元素。

标签: angular angular-animations


【解决方案1】:

将您的动画声明更改为:

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 不会播放离开动画。

【讨论】:

  • 所以没有办法执行请假动画?
  • @RaasMasood 有,你只需要确保你的动画是在你有 *ngIf 的元素上声明的。
  • stackoverflow.com/questions/47341321/…我在这里问了这个问题,看起来Angular只是不支持离开
猜你喜欢
  • 1970-01-01
  • 2018-01-10
  • 2018-11-08
  • 2019-10-03
  • 1970-01-01
  • 2018-10-22
  • 2018-09-01
  • 1970-01-01
  • 2018-03-01
相关资源
最近更新 更多