原文地址

https://www.jianshu.com/p/4400174072e2

大纲

  1、angular动画的相关概念
  2、angular动画的绑定方式
  3、angular动画代码实例

1、angular动画的相关概念

1.1、trigger(触发器)

<!--
所有的trigger都可以直接绑定element
像这样,只要state1 有改变,trigger都会触发。
在trigger世界里,是能控制state 和 transition。
-->
<div [@state]="state1" ></div>

1.2、state是转场的“场”,state(状态)

/*
1、*(通配符)状态:*(通配符)状态匹配任何动画状态。
当定义那些不需要管当前处于什么状态的样式及转场时,这很有用。 当该元素的状态从 active 变成任何其它状态时,active => * 转场都会生效。
当在任意两个状态之间切换时,* => * 转场都会生效。
* 通配符状态也能匹配 void。 2、void 状态
有一种叫做 void 的特殊状态,它可以应用在任何动画中。
它表示元素没有被附加到视图。
这种情况可能是由于它尚未被添加进来或者已经被移除了。
void 状态在定义“进场”和“离场”的动画时会非常有用。
比如当一个元素离开视图时,* => void 转场就会生效,
而不管它在离场以前是什么状态。 3、还有一些:enter、:leave等状态是angular本身自带的,
比如:enter代表路由进场、:leave代表路由出场
*/
state("void", style({ height: 0 })) //void是保留字,是没有东西
state("*", style({ height: 0 })) //*是保留字,是default
state("closed", style({ height: 0 }))
state("open, visible", style({ height: "*" }))

1.3、transition是转场,state去到下一个state

/**
转场的书写方式有很多种,如下是一些常见的转场
**/
transition("on => off", animate(500)),
transition("on <=> off", animate(500)),
transition("on => off, off => void", animate(500)),
transition("void => *", animate(500)),
transition("* => *", animate("1s 0s")),
transition((fromState, toState) => {
return fromState == "off" && toState == "on";
}, animate("1s 0s"))
transition(":enter", animate(500)),
transition(":leave", animate(500)),

1.4、基础动画animate

/*
animate 使用方式就很多 (可以控制过渡时长,延迟,和过渡动作,结束动画)
动画的方式主要有两种:
1、简单的转场方式,
通过自定义的两种状态之间的样式转换从而形成动画
2、复杂的转场方式,
通过自定义动画帧的方式从而详细定义动画不同时间状态下的转场方式
*/
animate(500, style(...))
animate("1s", style(...))
animate("100ms 0.5s", style(...))
animate("5s ease", style(...))
animate("5s 10ms cubic-bezier(.17,.67,.88,.1)", style(...))
animate(500, style({ background: "red" }))
animate(500, keyframes([
style({ background: "blue" })),
style({ background: "red" }))

1.5、动画时间线

/*
对每一个动画转场效果,有三种时间线属性可以调整:
持续时间(duration)、延迟(delay)和缓动(easing)函数。
它们被合并到了一个单独的转场时间线字符串。 持续时间:持续时间控制动画从开始到结束要花多长时间。 延迟:延迟控制的是在动画已经触发但尚未真正开始转场之前要等待多久。 缓动函数:缓动函数用于控制动画在运行期间如何加速和减速。
比如:使用 ease-in 函数意味着动画开始时相对缓慢,然后在进行中逐步加速。
可以通过在这个字符串中的持续时间和延迟后面添加第三个值来控制使用哪个缓动函数
(如果没有定义延迟就作为第二个值)。
*/

1.6、并行动画组(Group)

/*
你可能会希望为同时发生的几个动画配置不同的时间线。
比如,同时对两个 CSS 属性做动画,但又得为它们定义不同的缓动函数。 这种情况下就可以用动画组来解决了。
在这个例子中,同时在进场和离场时使用了组,以便能让它们使用两种不同的时间线配置
它们被同时应用到同一个元素上,但又彼此独立运行:
*/
animations: [
trigger('flyInOut', [
state('in', style({width: 120, transform: 'translateX(0)', opacity: 1})),
transition('void => *', [
style({width: 10, transform: 'translateX(50px)', opacity: 0}),
group([
animate('0.3s 0.1s ease', style({
transform: 'translateX(0)',
width: 120
})),
animate('0.3s ease', style({
opacity: 1
}))
])
]),
transition('* => void', [
group([
animate('0.3s ease', style({
transform: 'translateX(50px)',
width: 10
})),
animate('0.3s 0.2s ease', style({
opacity: 0
}))
])
])
])
]

2、angular动画的绑定方式

2.1在当前组件中自定义动画,然后使用

2.1.1 在组件中定义动画

import {
Component, OnInit,
trigger, state, style, transition,
animate
} from '@angular/core';
@Component({
selector: 'test-animation',
templateUrl: './test-animation.component.html',
styleUrls: ['./test-animation.component.css'],
animations:[
//在position状态改变时,触发动画
trigger('position',[
//position为left时的样式
state('left',style({
'margin-left': '0',
'background-color':'yellow'
})),
//position为right时的样式
state('right',style({
'margin-left': '200px',
'background-color':'blue'
})),
//状态切换时的动画设置
transition('left => right',animate('1000ms ease-in')),
transition('right => left',animate('1000ms ease-out'))
])
]
})
export class TestAnimationComponent implements OnInit {
currentPosition = 'left';
constructor() { }
ngOnInit() {
}
/**
* 按钮事件,切换状态
*/
togglePosition() {
if(this.currentPosition === 'left') {
this.currentPosition = 'right';
}else {
this.currentPosition = 'left';
}
}
}

2.1.2、在模板中绑定使用

<div id="brick" [@position]="currentPosition"></div>
<button (click)="togglePosition()">切换位置</button>

2.1.3、通过事件改变状态,从而触发动画效果

  当传入的状态由left转变为right的时候或者从right转变为left的时候都会触发相应的状态动画

2.2、在非当前组件中自定义动画,然后引入并使用

2.2.1、定义一个自定义动画

import {
animate, state,
style, transition, trigger, keyframes,
query, stagger
} from '@angular/animations'; // Component transition animations
export const flyInOut =
trigger('flyInOut', [
state('in', style({height:'0'})),
state('out', style({height:'100px'})),
transition('in => out', [
animate(2000, keyframes([
style({ height: '0', opacity: 0, offset: 0, display:'block'}),
style({ height: '*', opacity: 1, offset: 1, display:'none'})
]))
]),
transition('out => in', [
animate(2000, keyframes([
style({ height: '*', opacity: 1, offset: 0 }),
style({ height: '0', opacity: 0, offset: 1 })
]))
]),
])

2.2.2、在组件中引入并调用

import { Component } from "@angular/core";
import { flyInOut } from "../share/animation/flyInOut.animations"; @Component({
selector:'test-animation2',
templateUrl:'./test-animation2.component.html',
styleUrls:['./test-animation2.component.css'],
animations:[flyInOut]
}) export class TestAnimation2Component {
flyState = 'in'; changeState() {
if(this.flyState === 'in'){
this.flyState = 'out'
}else {
this.flyState = 'in'
}
}
}

2.2.3、在模板中绑定动画并通过事件改变动画状态从而调用转场动画

<!--
.block {
width: 100px;
height: 100px;
background-color: blueviolet;
}
-->
<div class="block" [@flyInOut]="flyState">
</div>
<button (click)="changeState()">
change state
</button>

3、angular动画代码实例

/*
testAnimation.component.ts
*/
import {
Component, OnInit, trigger,
state, style, transition, animate
} from '@angular/core';
@Component({
selector: 'test-animation',
templateUrl: './testAnimation.component.html',
styleUrls: ['./testAnimation.component.scss'],
animations:[
//在position状态改变时,触发动画
trigger('position',[
//position为left时的样式
state('left',style({
'margin-left': 0,
'background-color':'yellow'
})),
//position为right时的样式
state('right',style({
'margin-left': 200,
'background-color':'blue'
})),
//状态切换时的动画设置
transition('left => right',animate('1000ms ease-in')),
transition('right => left',animate('1000ms ease-out'))
])
]
})
export class TestAnimationComponent implements OnInit {
currentPosition = 'left';
constructor() { }
ngOnInit() {
}
/**
* 按钮事件,切换状态
*/
togglePosition() {
if(this.currentPosition === 'left') {
this.currentPosition = 'right';
}else {
this.currentPosition = 'left';
}
}
}
<!--
testAnimation.component.html
-->
<div id="brick" [@position]="currentPosition"></div>
<button (click)="togglePosition()">切换位置</button>
//testAnimation.component.scss
#brick {
width: 20rem;
height: 10rem;
background-color: aqua;
margin-left: 0;
}

  

代码资料

angular示例代码中的angular-animation,里面不仅有本教程我讲解的代码实例,还有我自定义的一些动画的使用,希望能给各位提供帮助。

angular动画知识点以及代码样例的更多相关文章

  1. 2020JAVA最新应对各种OOM代码样例及解决办法

    引言 作者:黄青石 链接:https://www.cnblogs.com/huangqingshi/p/13336648.html?utm_source=tuicool&utm_medium= ...

  2. 33个超级有用必须要收藏的PHP代码样例

    作为一个正常的程序员,会好几种语言是十分正常的,相信大部分程序员也都会编写几句PHP程序,如果是WEB程序员,PHP一定是必备的,即使你没用开发过大型软件项目,也一定多少了解它的语法. 在PHP的流行 ...

  3. java servlet 代码样例 (demo)

    今天又搞了下jsp +servlet 的代码样例,感觉虽然搭了好多次,可是每次还是不记得那些参数,都要去网上搜索,索性自己把这次的简单demo给记录下来,供下次使用的时候直接复制吧. 这个web逻辑 ...

  4. zookeeper实战:SingleWorker代码样例

    我们需要一个“单点worker”系统,此系统来确保系统中定时任务在分布式环境中,任意时刻只有一个实例处于活跃:比如,生产环境中,有6台机器支撑一个应用,但是一个应用中有30个定时任务,这些任务有些必须 ...

  5. 30个php操作redis经常用法代码样例

    这篇文章主要介绍了30个php操作redis经常用法代码样例,本文事实上不止30个方法,能够操作string类型.list类型和set类型的数据,须要的朋友能够參考下 redis的操作非常多的,曾经看 ...

  6. JAVA各种OOM代码样例及解决方法

    周末了,觉得我还有很多作业没有写,针对目前大家对OOM的类型不太熟悉,那么我们来总结一下各种OOM出现的情况以及解决方法. 我们把各种OOM的情况列出来,然后逐一进行代码编写复现和提供解决方法. 1. ...

  7. PHP代码样例

    1 <?php 2 3 /** 4 * 时间:2015-8-6 5 * 作者:River 6 * 超级有用.必须收藏的PHP代码样例 7 */ 8 class Helper { 9 10 /** ...

  8. Java操作HDFS代码样例

    代码在GitHub上. 包括如下几种样例代码: 新建文件夹 删除文件/文件夹 重命名文件/文件夹 查看指定路径下的所有文件 新建文件 读文件 写文件 下载文件至本地 上传本地文件 https://gi ...

  9. Python代码样例列表

    扫描左上角二维码,关注公众账号 数字货币量化投资,回复“1279”,获取以下600个Python经典例子源码 ├─algorithm│       Python用户推荐系统曼哈顿算法实现.py│    ...

随机推荐

  1. Method and apparatus for transitioning between instruction sets in a processor

    A data processor (104) is described. The data processor (104) is capable of decoding and executing a ...

  2. 计算机视觉(ComputerVision, CV)相关领域的站点链接

    关于计算机视觉(ComputerVision, CV)相关领域的站点链接,当中有CV牛人的主页.CV研究小组的主页,CV领域的paper,代码.CV领域的最新动态.国内的应用情况等等. (1)goog ...

  3. 设计模式(7)-结构型模式-Bridge模式

    2.结构性模式 2.2  BRIDGE模式 别名:handle/body 这个模式体现了组合相对于继承的优势. 2.2.1动机 当一个抽象可能有多个实现时,通经常使用继承来协调它们.抽象类定义对该抽象 ...

  4. c# 查询sql 返回多个參数

    1.依据须要查询mysql 语句,返回三个须要的參数,不是数据集 2.编写函数例如以下: public static void GetParas(string 条件1, out string 返回值1 ...

  5. jquery--this

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  6. [Vue + TS] Create Type-Safe Vue Directives in TypeScript

    Directives allow us to apply DOM manipulations as side effects. We’ll show you how you can create yo ...

  7. 感谢党,软考过了。系统集成项目管理project师

    人品爆发了,刚用干巴巴的日语做完2小时的设计说明,回到家一查,人品爆发了.软考竟然过了. 绝对是评卷老师给人品啊!真想请他吃顿饭. 系统集成项目管理project师  64 53 幸运飞过! 今天真是 ...

  8. js遍历对象的属性和方法

    js遍历对象的属性和方法 一.总结 二.实例 练习1:具有默认值的构造函数 实例描述: 有时候在创建对象时候,我们希望某些属性具有默认值 案例思路: 在构造函数中判断参数值是否为undefined,如 ...

  9. .condarc(conda 配置文件)

    Configuration - Conda documentation .condarc以点开头,一般表示 conda 应用程序的配置文件,在用户的家目录(windows:C:\\users\\use ...

  10. 5.decltype类型拷贝

    #include <iostream> using namespace std; template <class T> void show(T *p) { //初始化 decl ...