zoukankan      html  css  js  c++  java
  • [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`))
      ]);
    };
  • 相关阅读:
    Hbase的数据目录更换后server is not running yet报错
    挂载新加4T硬盘到home目录
    Hadoop(二)--Hadoop运行模式
    Hadoop(一)--Hadoop框架介绍
    KubeSphere(四)--Devops工程pipeline
    KubeSphere(三)--示例:安装wordpress到k8s
    KubeSphere(二)--多租户管理权限控制
    KubeSphere(一)--基于k8s安装KubeSphere
    com.alibaba.fastjson.JSON.toJSONString使用时值为NULL的属性被忽略的问题
    数据库命令-实战
  • 原文地址:https://www.cnblogs.com/Answer1215/p/7242790.html
Copyright © 2011-2022 走看看