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="width: 200px; height: 200px; background: black;">
</section> <section [@heightState]="state" style="width: 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:
}),
animate(
"{{duration}} ease-in",
style({
height: '*'
})
)
]); const heightEnd = animation([
animate(
"{{duration}} ease-out",
style({
height:
})
)
]); /*
* 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:
})),
state('active', style({
height: params.height
})),
transition('inactive => active', animate(`${params.duration} ease-in`)),
transition('active => inactive', animate(`${params.duration} ease-out`))
]);
};

[Angular] Two ways to create Angular Animation, using animation() or using state()的更多相关文章

  1. angular源码分析:angular的整个加载流程

    在前面,我们讲了angular的目录结构.JQLite以及依赖注入的实现,在这一期中我们将重点分析angular的整个框架的加载流程. 一.从源代码的编译顺序开始 下面是我们在目录结构哪一期理出的an ...

  2. angular源码分析:angular中各种常用函数,比较省代码的各种小技巧

    angular的工具函数 在angular的API文档中,在最前面就是讲的就是angular的工具函数,下面列出来 angular.bind //用户将函数和对象绑定在一起,返回一个新的函数 angu ...

  3. angular源码分析:angular中的依赖注入式如何实现的

    一.准备 angular的源码一份,我这里使用的是v1.4.7.源码的获取,请参考我另一篇博文:angular源码分析:angular源代码的获取与编译环境安装 二.什么是依赖注入 据我所知,依赖注入 ...

  4. [Angular] Use ngx-build-plus to compile Angular Elements

    We can treat Angular Element as each standlone lib and compile each Angular element spreatly. Tool w ...

  5. angular源码分析:angular中脏活累活的承担者之$interpolate

    一.首先抛出两个问题 问题一:在angular中我们绑定数据最基本的方式是用两个大括号将$scope的变量包裹起来,那么如果想将大括号换成其他什么符号,比如换成[{与}],可不可以呢,如果可以在哪里配 ...

  6. angular源码分析:angular中入境检察官$sce

    一.ng-bing-html指令问题 需求:我需要将一个变量$scope.x = '<a href="http://www.cnblogs.com/web2-developer/&qu ...

  7. angular的跨域(angular百度下拉提示模拟)和angular选项卡

    1.angular中$http的服务: $http.get(url,{params:{参数}}).success().error(); $http.post(url,{params:{参数}}).su ...

  8. angular源码分析:angular中jqLite的实现——你可以丢掉jQuery了

    一.从function JQLite(element)函数开始. function JQLite(element) { if (element instanceof JQLite) { //情况1 r ...

  9. angular源码分析:angular的源代码目录结构说明

    一.读源码,是选择"编译合并后"的呢还是"编译前的"呢? 有朋友说,读angular源码,直接看编译后的,多好,不用管模块间的关系,从上往下读就好了.但是在我看 ...

随机推荐

  1. 数据库SQL Server2012笔记(四)——多表查询、子查询、分页查询、用查询结果创建新表和外连接

    1.多表查询 1)笛卡尔集: select  *  from  表名1,表名2 select  *  from  表名1.表名2  where   表名1.字段名=表名2.字段名 注: 若有两张表有同 ...

  2. 设计模式之Build(生成者模式)

    一.生成器模式的定义: 生成器模式也称为建造者模式.生成器模式的意图在于将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示(GoF).在软件设计中,有时候面临着一个非常复杂的对象的创 ...

  3. HTML标签大全(常用)

    转自:https://www.cnblogs.com/moije/p/6751624.html 文本标记语言,即HTML(Hypertext Markup Language),是用于描述网页文档的一种 ...

  4. 3.索引与string进行映射实现高效查找

    #include <iostream> #include <typeindex>//类型索引 #include <unordered_map>//红黑树 #incl ...

  5. SSH远程快速登录Linux

    SSH远程快速登录Linux        使用SSH管理linux服务器,通常要使用ssh,然后输入用户,密码,其实只要配置一个文件就可以方便登录.假设要登录server域名是www.interne ...

  6. ASP.NET MVC Web API 学习笔记---第一个Web API程序---近来很多大型的平台都公开了Web API

    1. Web API简单说明 近来很多大型的平台都公开了Web API.比如百度地图 Web API,做过地图相关的人都熟悉.公开服务这种方式可以使它易于与各种各样的设备和客户端平台集成功能,以及通过 ...

  7. pycharm做什么

    pycharm做什么 说实话.作为一个Coder.每天在各种IDE中切换编写Code.如果一个IDE Look and Feel总是无形中影响你每天Code Farm的心情.那该是多么不爽的事情.特别 ...

  8. 最大子矩阵和 51Nod 1051 模板题

    一个M*N的矩阵,找到此矩阵的一个子矩阵,并且这个子矩阵的元素的和是最大的,输出这个最大的值. 例如:3*3的矩阵:   -1 3 -1 2 -1 3 -3 1 2   和最大的子矩阵是:   3 - ...

  9. Day 3 EX 购物车自写

    # -*- coding: utf_8 _*_# Author:Vi import copygoods = [0,[1,'iphone',20],[2,'ipad',2500]]salary = in ...

  10. RecipientsEditor-信息收件人输入框

    首先说一下信息收件人这个类的继承关系 RecipientsEditor->EncapsulatedMTKRecipientEditTextView(这两个类都在mms目录下) ->MTKR ...