Angular Animation For Dynamically Changing Height

后端 未结 4 1185
日久生厌
日久生厌 2020-12-14 02:56

I\'ve successfully gotten a panel to animate expanding and closing when entering and leaving the DOM. The problem is I now have a busy indicator inside the panel prior to sh

4条回答
  •  借酒劲吻你
    2020-12-14 03:51

    I've written a component that smoothly animates the height of projected content if that content changes. It's used like this:

    
      {{content}}
    
    

    Here's a stackblitz: https://stackblitz.com/edit/angular4-kugxw7

    This is the component:

    import {ElementRef, HostBinding, Component, Input, OnChanges} from '@angular/core';
    import {animate, style, transition, trigger} from "@angular/animations";
    
    @Component({
      selector: 'smooth-height',
      template: `
        
      `,
      styles: [`
        :host {
          display: block;
          overflow: hidden;
        }
      `],
      animations: [
        trigger('grow', [
          transition('void <=> *', []),
          transition('* <=> *', [
            style({height: '{{startHeight}}px', opacity: 0}),
            animate('.5s ease'),
          ], {params: {startHeight: 0}})
        ])
      ]
    })
    export class SmoothHeightComponent implements OnChanges {
      @Input()
      trigger: any;
    
      startHeight: number;
    
      constructor(private element: ElementRef) {}  
    
      @HostBinding('@grow') get grow() {
        return {value: this.trigger, params: {startHeight: this.startHeight}};
      }
    
      setStartHeight(){
        this.startHeight = this.element.nativeElement.clientHeight;
      }
    
      ngOnChanges(){
        this.setStartHeight();
      }
    }
    

提交回复
热议问题