Slide down animation Angular 4

前端 未结 3 1026
忘掉有多难
忘掉有多难 2021-02-05 14:01

I am trying to animate my page but have the following issue:
I have content div on my page, and a button that opens another div above the content. I would like that div to

相关标签:
3条回答
  • 2021-02-05 14:30

    This is a clean and easy way to implement a slide down animation in angular 2+

    my-component.ts

    import { animate, style, transition, trigger } from '@angular/animations';
    @Component({
      animations: [
        trigger('slideDownUp', [
          transition(':enter', [style({ height: 0 }), animate(500)]),
          transition(':leave', [animate(500, style({ height: 0 }))]),
        ]),
      ],
    })
    

    my-component.html

    <div @slideDownUp *ngIf="isShowing" class="box">
      I am the content of the div!
    </div>
    

    my-component.scss

    .box {
      overflow: hidden;
    }
    
    0 讨论(0)
  • 2021-02-05 14:36

    First, create a file where you would define your animations and export them. Just to make it more clear in your app.component.ts

    In the following example, I used a max-height of the div that goes from 0px (when it's hidden), to 500px, but you would change that according to what you need.

    This animation uses states (in and out), that will be toggle when we click on the button, which will run the animtion.

    animations.ts

    import { trigger, state, style, transition,
        animate, group, query, stagger, keyframes
    } from '@angular/animations';
    
    export const SlideInOutAnimation = [
        trigger('slideInOut', [
            state('in', style({
                'max-height': '500px', 'opacity': '1', 'visibility': 'visible'
            })),
            state('out', style({
                'max-height': '0px', 'opacity': '0', 'visibility': 'hidden'
            })),
            transition('in => out', [group([
                animate('400ms ease-in-out', style({
                    'opacity': '0'
                })),
                animate('600ms ease-in-out', style({
                    'max-height': '0px'
                })),
                animate('700ms ease-in-out', style({
                    'visibility': 'hidden'
                }))
            ]
            )]),
            transition('out => in', [group([
                animate('1ms ease-in-out', style({
                    'visibility': 'visible'
                })),
                animate('600ms ease-in-out', style({
                    'max-height': '500px'
                })),
                animate('800ms ease-in-out', style({
                    'opacity': '1'
                }))
            ]
            )])
        ]),
    ]
    

    Then in your app.component, we import the animation and create the method that will toggle the animation state.

    app.component.ts

    import { SlideInOutAnimation } from './animations';
    
    @Component({
      ...
      animations: [SlideInOutAnimation]
    })
    export class AppComponent  {
      animationState = 'in';
    
      ...
    
      toggleShowDiv(divName: string) {
        if (divName === 'divA') {
          console.log(this.animationState);
          this.animationState = this.animationState === 'out' ? 'in' : 'out';
          console.log(this.animationState);
        }
      }
    }
    

    And here is how your app.component.html would look like :

    <div class="wrapper">
      <button (click)="toggleShowDiv('divA')">TOGGLE DIV</button>
      <div [@slideInOut]="animationState" style="height: 100px; background-color: red;">
      THIS DIV IS ANIMATED</div>
      <div class="content">THIS IS CONTENT DIV</div>
    </div>
    

    slideInOut refers to the animation trigger defined in animations.ts

    Here is a StackBlitz example I have created : https://stackblitz.com/edit/angular-muvaqu

    Side note : If an error ever occurs and asks you to add BrowserAnimationsModule, just import it in your app.module.ts:

    import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
    
    @NgModule({
      imports: [ ..., BrowserAnimationsModule ],
      ...
    })
    
    0 讨论(0)
  • 2021-02-05 14:44

    I quite prefer the wildcard operator when working with height transitions to allow for dynamic height content.

    // Bind to true/false states via 0 and 1 values
    
    trigger('slideUpDown', [
      state('0', style({ 'max-height': '*', opacity: 1 })),
      state('1', style({ 'max-height': '0px', opacity: 0 })),
      transition(':enter', animate('400ms ease-in-out')),
      transition('* => *', animate('400ms ease-in-out')),
    ])
    

    usage:

    <div #someDiv [@slideUpDown]="someDiv.state"></div>
    

    somewhere else or in the template you can toggle the state.

    <button (click)="someDiv.state = !someDiv.state"></button>
    
    0 讨论(0)
提交回复
热议问题