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. 2011年度十大杰出IT博客获奖感言

        2011年度十大杰出IT博客获奖感言 在各位评委.网友的支持下,我的博客从前50名中脱颖而出跻身10强,得到这个消息之后心中充满了喜悦.在这里要感谢51CTO为大家提供这样一个良好的展示平台. ...

  2. javafx tabPane

    public class EffectTest extends Application { @Override public void start(Stage primaryStage) { prim ...

  3. Font-Awesome最新版完整使用教程

    何为Font-Awesome Font Awesome gives you scalable vector icons that can instantly be customized - size, ...

  4. FormatMessage函数的使用方法

    使用FormatMessage时假设对一些參数不细致研究.那么就会出错误.首先说下这个函数 1 函数描写叙述 DWORD WINAPI FormatMessage( _In_ DWORD dwFlag ...

  5. java语言建立顺序表

    package datastructure; //线性表 public interface IList { public void clear(); public boolean isEmpty(); ...

  6. 9.使用 npm 命令安装模块

    转自:http://www.runoob.com/nodejs/nodejs-tutorial.html npm 安装 Node.js 模块语法格式如下: $ npm install <Modu ...

  7. 虚拟机上的企业网络管理系统(cisco works 2000安装配置)

    虚拟机上的企业网络管理系统 北京 李晨光 相关文章 Cisco Works 2000 网络管理软件安装.配置全过程 http://you.video.sina.com.cn/b/18168631-14 ...

  8. CF-558E (线段树/分块)

    解题思路: 很显然突破口就是字符集比较小,分块和线段树都能A 话说线段树时间是分块5倍啊 代码(线段树): #include<cstdio> #include<cstring> ...

  9. Linux启动过程总结

    当我们按开机键后,主机就会执行: 1.POST(Power-On Self Test 加电自检). 2.读取BIOS中定义的开机设备启动程序,并加载MBR(主引导记录(Master Boot Reco ...

  10. 洛谷——P1042 乒乓球

    https://www.luogu.org/problem/show?pid=1042 题目背景 国际乒联现在主席沙拉拉自从上任以来就立志于推行一系列改革,以推动乒乓球运动在全球的普及.其中11分制改 ...