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. mongoDB数据库插入数据时报错:db.collection is not a function

    nodejs连接mongodb插入数据时,发现mongoDB报错:db.collection is not a function.解决方法: 1.npm下载mongodb2.x.x版本替换3.x.x ...

  2. A - Shashlik Cooking CodeForces - 1040B

    http://codeforces.com/problemset/problem/1040/B Long story short, shashlik is Miroslav's favorite fo ...

  3. [Day11]接口、多态

    1.接口 (1)接口定义:interface关键字 ,所在文件仍然是.java文件,编译后仍产生.class文件.       定义格式 public interface 接口名{ 抽象方法1: 抽象 ...

  4. Docker入门基础(一)

    Docker入门基础 Linux只存在文件目录,不存在“盘”的概念 Dockers优点:方便部署环境.资源占用少(微服务) Docker的三大概念 镜像:类似虚拟机的镜像.用俗话说就是安装文件.容器: ...

  5. php 连接 数据库

    $mysql_server_name='localhost'; //改成自己的mysql数据库服务器 $mysql_username='root'; //改成自己的mysql数据库用户名 mysql默 ...

  6. Android ListVeiw控件(转载整理)

    列表的显示需要三个元素: 1.ListVeiw 用来展示列表的View. 2.适配器 用来把数据映射到ListView上的中介. 3.数据    具体的将被映射的字符串,图片,或者基本组件. 根据列表 ...

  7. 把ResNet-L152模型的ckpt文件转化为pb文件

    import tensorflow as tf from tensorflow.python.tools import freeze_graph #os.environ['CUDA_VISIBLE_D ...

  8. 转载 Unity Text 插入超链接

    using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressi ...

  9. VCS

    timing check相关的, +notimingcheck命令,可以用在compile时,也可以用在run time的时候, 都是将检查timing的系统函数,都disable掉了, 加在comp ...

  10. extends 与implements的区别和用法

    1. 在类的声明中,通过关键字extends来创建一个类的子类.一个类通过关键字implements声明自己使用一个或者多个接口. extends 是继承某个类, 继承之后可以使用父类的方法, 也可以 ...