Angular练习之animations动画二
返回目录 回顾
引入动画模块>创建动画对象>在动画载体上使用。我觉得其核心的内容在创建动画对象上,今天我们就来练习创建不同的动画对象trigger
开始练习创建例子2ng g component my-animations/exp2 布局 <h1>动画实例2</h1> <div> <button (click)="changState('left')">状态变成左</button> <button (click)="changState('right')">状态变成右</button> <button (click)="changState('')">状态为空</button> <button (click)="changState('center')">状态为中</button> </div> <br> <div style="height: 100px;width: 100px;background-color: black" [@box]="boxState"></div> ts import { Component,OnInit } from '@angular/core'; import {boxAnimate2} from "../animations" @Component({ selector: 'app-exp2',templateUrl: './exp2.component.html',animations: [ boxAnimate2 ] }) export class Exp2Component implements OnInit { // 状态 private boxState=''; constructor() { } ngOnInit() { } changState(state){ this.boxState = state; } } 写动画效果,只定义状态。
// 定义一个动画,左右两个状态加上颜色变化更容易理解吧 export const boxAnimate2 = trigger('box',[ // 定义一个状态left state('left',style({ transform: 'translate3d(0,0)',background:'red' })),// 定义另外一个状态right state('right',style({ transform: 'translate3d(200%,background:'blue' })),]);
添加任意动画过程transition// 定义运动过程(从任意到任意) transition('* => *',animate(500)),
入场动画void => *
// 入场动画 transition("void => *",[ style({ opacity: 0,transform: 'translate3d(200%,200%,0)'}),animate(500) ]),// 定义运动过程(从任意到任意) transition('* => *',
":enter"和":leave"
我们添加一个按钮,修改布局如下: <button (click)="changShow()">显示/隐藏</button> <div *ngIf="show" style="height: 100px;width: 100px;background-color: black" [@box]="boxState"></div> ts private show= false; changShow(){ this.show=!this.show; }
修改动画效果 //入场动画 transition(":enter",// 出场动画 transition(":leave",[ style({ opacity: 1}),animate(500,style({ opacity: 0,0)'})) ]), 小结
基于关键帧(Keyframes)的多阶段动画
这里布局和ts代码我就跳过了。主要看transition的定义和效果 export const KeyframesAnimate = trigger('KeyframesAnimate',[ //入场动画 transition(":enter",[ animate(500,keyframes([ style({opacity: 0,transform: 'translate3d(-400%,offset: 0}),style({opacity: 0.5,transform: 'translate3d(-150%,-50%,offset: 0.3}),style({opacity: 1,transform: 'translate3d(0,10%,offset: 0.7}),offset: 1.0}) ])) ]),keyframes([ style({opacity: 1,transform: 'translate3d(150%,style({opacity: 0,transform: 'translate3d(400%,]);
并行动画组(Group)export const GroupAnimate = trigger('GroupAnimate',width: '0px',height: '0px',transform: 'translateX(-200%)'}),group([ animate('1s ease',style({transform: 'translateX(0)'})),animate('1s 200ms ease',style({width: '100px'})),style({height: '100px'})),animate('0.5s',style({opacity: 1})),]) ]),]);
总结
源码源码放在github开源社区上面,随时会更新。所以你下载最新版本的时候会与该文章描述的略有差异。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- angularjs – 如何在ui-router中继承解析数据
- Bootstrap_01(入门 教程)
- shell 学习五十二天----删除进程 kill 命令
- node.js – 无法连接到docker mongodb
- Bootstrap源码之旅-CSS环境搭建
- (webservice+cxf+mybatis+mysql+springmvc) webs
- 【Angular2】You have to be inside an angular-
- 将新的Iterable {}代码从Scala 2.7.7移植到2.8
- AngularJS转到状态而不重新加载并且没有值的参数
- 在Twitter Bootstrap中如何使用CSS将文本与元素的