• [Angular] Two ways to create Angular Animation, using animation() or using state()


    We have two blocks to show to difference ways to do animation in Angular:

    <button (click)="toggleState()">Toggle</button>
    
    <div style="display: flex; align-items: center; align-content: space-between;">
    
      <section [@heightZeroFull] *ngIf="state === 'active'" style=" 200px; height: 200px; background: black;">
      </section>
    
      <section [@heightState]="state" style=" 200px; height: 200px; background: blue;"></section>
    </div>

    heightZeroFull using animation(). heightState using state().

    The way to control the state is:

      state = 'active';
    
      toggleState() {
        if (this.state === 'inactive') {
          this.state = 'active';
        } else {
          this.state = 'inactive';
        }
      }

    In the component, we define 'animations':

    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css'],
      animations: [
        heightZeroFull({duration: '500ms'}),
        heightState({height: '200px', duration: '500ms'})
      ]
    })

    We pass in params, so it is more configurable.

    Animation.ts:

    import {animation, style, animate, trigger, transition, useAnimation, state} from '@angular/animations';
    
    /*
    * DSL
    * */
    const heightStart =  animation([
      style({
        height: 0
      }),
      animate(
        "{{duration}} ease-in",
        style({
          height: '*'
        })
      )
    ]);
    
    const heightEnd = animation([
      animate(
        "{{duration}} ease-out",
        style({
          height: 0
        })
      )
    ]);
    
    
    /*
    * Transition
    * */
    // Using animation
    export const heightZeroFull = (params) => {
      return trigger('heightZeroFull', [
        transition(':enter', useAnimation(heightStart, {params})),
        transition(':leave', useAnimation(heightEnd, {params}))
      ]);
    };
    
    
    // Using state
    export const heightState = (params) => {
      return trigger('heightState', [
        state('inactive', style({
          height: 0
        })),
        state('active',   style({
          height: params.height
        })),
        transition('inactive => active', animate(`${params.duration} ease-in`)),
        transition('active => inactive', animate(`${params.duration} ease-out`))
      ]);
    };
  • 相关阅读:
    Django-ORM
    深入理解vue 修饰符sync
    PS切图
    用Chrome 浏览器调试移动端网页 chrome://inspect/#devices
    float浮动导致父元素高度坍塌的原因及清除浮动方法
    vue keep-alive
    ES6 箭头函数
    ES6 Module(模块)
    MVC模式 和 MVVM模式
    移动端适配代码
  • 原文地址:https://www.cnblogs.com/Answer1215/p/7242790.html
Copyright © 2020-2023  润新知