import {
Component, Input, OnInit
} from '@angular/core';
import {
trigger, state, style, animate, transition, group, sequence, keyframes, useAnimation, stagger, animateChild, query, animation
} from '@angular/animations'; var fadeAnimation = animation([
style({ opacity: '{{ start }}' }),
animate('{{ time }}', style({ opacity: '{{ end }}' }))
], { params: { time: '1000ms', start: 0, end: 1 } }); @Component({
selector: 'app-animate',
templateUrl: './animate.component.html',
styleUrls: ['./animate.component.css'],
animations: [
trigger('state', [
state('pointA', style({
left: '0%'
})),
transition('pointA => pointB', [
style({
border: "3px solid #000"
}),
animate(1000, style({
left: "100%"
})),
group([
//basic animation
]),
sequence([
//basic animation
]),
query(":leave", [
//basic animation
]),
query(":leave", stagger(1000, [
//basic animation
])),
useAnimation(fadeAnimation, {
params: {
time: '2s',
start: 1,
end: 0
}
})
])
])
]
})
export class AnimateComponent implements OnInit {
state = 'pointA';
constructor() { }
ngOnInit() {
}
}

  

源码链接:https://github.com/angular/angular/blob/master/packages/animations/src/animation_metadata.ts

在开始前,我们先不管如何写动画,先看trigger是什么,所有的trigger都可以直接绑定element

<div [@state]="state1" ></div>

像这样,只要state1 有改变,trigger都会触发。
在trigger世界里,是能控制state 和 transition。

state是转场的“场”

state("void", style({ height: 0 })) //void是保留字,是没有东西
state("*", style({ height: 0 })) //*是保留字,是default
state("closed", style({ height: 0 }))
state("open, visible", style({ height: "*" }))

    

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)),

  

当了解state 和 transition 后,我们需要了解基础动画 style 和 animate

style 使用方式和css一样,这里就不多说

animate 使用方式就很多 (可以控制过渡时长,延迟,和过渡动作,结束动画)

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" }))
])

animate 对象里可以有style 和 keyframe,懂css的都明白。

动画基础讲述的是style 和 animate, 高级动画是group, sequence, query,stagger。所有的高级都基于动画基础的组装。

group是把多个基础给包在一起,同时触发

group([
animate("1s", { background: "black" }))
animate("2s", { color: "white" }))
])

  

sequence就相对的,是一个一个触发。(默认功能)

 sequence([
style({ opacity: 0 })),
animate("1s", { opacity: 1 }))
])

如果你不要特地包一个sequence,你可以直接写基础动画,效果是一样的

query是可以寻找指定的子层进行动画,寻找的方式有

 query('div', [
animate(...),
animate(...)
], { limit: 1 }) query('.some-element-that-may-not-be-there', [
animate(...),
animate(...)
], { optional: true }) query(':self, .record:enter, .record:leave, @subTrigger', [...]) - Querying for newly inserted/removed elements using `query(":enter")`/`query(":leave")`
- Querying all currently animating elements using `query(":animating")`
- Querying elements that contain an animation trigger using `query("@triggerName")`
- Querying all elements that contain an animation triggers using `query("@*")`
- Including the current element into the animation sequence using `query(":self")`

limit 是声明限制element的数量, optional 是如果query可能没有找到,就得声明

:enter / :leave 等等都是特别的表达寻找式,这里不多说

stagger 是间隔,通常用在ngFor,场景是当你需要删除多个元素时,你需要每一个元素都是间隔离开,而不是同时离开

query(':leave', [
stagger(100, [
animate('0.5s', style({ opacity: 0 }))
])
]),
query(':enter', [
style({ opacity: 0 }),
stagger(100, [
animate('0.5s', style({ opacity: 1 }))
])
])

调用query才能使用stagger,因为query才可以选着多个元素

animateChild() 这里有bug,在不调用这功能时,子层的trigger是不会触发的,但是目前就是会触发……

query('@childAnimation',
animateChild()
))
query('@childAnimation', stagger(100, [
animateChild()
]))

  

UseAnimate是一个调用方法,也是复用的部分

 var fadeAnimation = animation([
style({ opacity: '{{ start }}' }),
animate('{{ time }}',
style({ opacity: '{{ end }}'))
], { params: { time: '1000ms', start: 0, end: 1 }}); useAnimation(fadeAnimation, {
params: {
time: '2s',
start: 1,
end: 0
}
})

  

最后就是所有的基础动画都是可以组装的,即使我没有给出所有的例子。在组装一定是在array里 [],只要是逻辑就能装的,transition的例子就是这样。

angular 2 animation 结构笔记 version 4.2.2的更多相关文章

  1. [转]Angular项目目录结构详解

    本文转自:https://blog.csdn.net/yuzhiqiang_1993/article/details/71191873 版权声明:本文为博主原创文章,转载请注明地址.如果文中有什么纰漏 ...

  2. Angular 学习笔记 (version 6 小笔记)

    1. lazyload 的 path 变成相对路径了, 不过如果你用 ng update 的话, 依然可以不需要修改, cli config 好像能调支持绝对路径的写法. const routes: ...

  3. Angular项目目录结构

    前言:不支持MakeDown的博客园调格式的话,真的写到快o(╥﹏╥)o了,所以老夫还是转战到CSDN吧,这边就不更新啦啦啦~ CSDN地址:https://blog.csdn.net/Night20 ...

  4. 3.2 配置构建Angular应用——简单的笔记存储应用

    本节我们会通过构建一个简单的笔记存储应用(可以载入并修改一组简单的笔记)来学习如何应用Angular的特性.这个应用用到的特性有: 在JSON文件中存储笔记 展示.创建.修改和删除笔记 在笔记中使用M ...

  5. CSS3 Animation学习笔记

    Internet Explorer 9,以及更早的版本, 不支持 @keyframe 规则或 animation 属性. Internet Explorer 10.Firefox 以及 Opera 支 ...

  6. Angular Universal(统一平台)笔记

    angular官网高级文档AngularUniversal部分的翻译总结,这东西在angular4开始正式被官方支持了,目前其实支持的服务器端还没有很多,但好歹包括了node和DotNetCore,算 ...

  7. Angular的项目结构

    前面我们已经在我们想要的位置顺利的创建了Angular项目,现在我们就来看一下项目的结构吧. 下面使我们项目的整体结构,包括node的模板.src资源文件以及配置文件等. 下面是根目录文件夹内的文件用 ...

  8. angular $q的学习笔记转帖

    http://blog.segmentfault.com/bornkiller/1190000000402555 angular $q的一个不错的学习笔记

  9. Angular 5.x 学习笔记(2) - 生命周期钩子 - 暂时搁浅

    Angular 5.x Lifecycle Hooks Learn Note Angular 5.x 生命周期钩子学习笔记 标签(空格分隔): Angular Note on cnblogs.com ...

随机推荐

  1. UCloud首尔机房整体热迁移是这样炼成的

    小结: 1.把两个机房在逻辑上变成一个机房: 2.新老机房的后端服务使用同一套 ZooKeeper,但是配置的却是不同的 IP: 3.UCloud内部服务所使用的数据库服务为MySQL, 内部MySQ ...

  2. [math] sagemath

    官网首页:http://www.sagemath.org 首页里引出的两个教程 http://www.gregorybard.com/Sage.html http://sagebook.gforge. ...

  3. odoo二次开发 tips

    1.model属性 每个对象(即class)一般由字段(变量)和函数组成,每个对象对应着数据库中的一张表,驼峰命名方式 models.Model 基础模块,会根据字段和模型名在后台数据库生成对应得表文 ...

  4. Oracle11g 配置DG broker

    在配置DG broker之前需要确保Dataguard配置正常且主库和备库均使用spfile. 1. 主库配置 配置DG_BROKER_START参数 检查主库dg_broker_start设置 SQ ...

  5. C++11 std::ref使用场景

    C++本身有引用(&),为什么C++11又引入了std::ref(或者std::cref)? 主要是考虑函数式编程(如std::bind)在使用时,是对参数直接拷贝,而不是引用.如下例子: # ...

  6. java并发包消息队列(也即阻塞队列BlockingQueue)

    下面是典型的消息队列的生产者与消费者模式的例子

  7. grunt 打包 分解(并非原创)

    1. require('time-grunt')(grunt); Time how long tasks take. Can help when optimizing build times任务执行时 ...

  8. Zookeeper应用之——选举(Election)

    请注意,此篇文章并不是介绍Zookeeper集群内部Leader的选举机制,而是应用程序使用Zookeeper作为选举. 使用Zookeeper进行选举,主要用到了Znode的两个性质: 临时节点(E ...

  9. rsync 守护进程备份报错

    [root@nfs01 backup]# rsync -avz  /backup rsync_backup@172.16.1.41::backupPassword: @ERROR: auth fail ...

  10. supervisor 守护进程

    一.supervisor 安装 1.yum -y install epel-release 2.yum -y install supervisor 二.supervisor 配置文件详解 三.supe ...