Angular4.0动画操作实例详解

本文实例讲述了Angular4.0动画操作。分享给大家供大家参考,具体如下:

粗略的记录一下angular4的动画

先看一下angular中文网关于这个给的例子。

有两个组件home,about。 路径配置什么的这里就不细说了,之前的博文有说过,我就贴一下代码,很好理解的,

需要import的东西我先说一下,我只贴了使用动画要用的东西,其他的我省略了,

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
@NgModule({
  ...
 imports: [
 BrowserModule,
 BrowserAnimationsModule,
 AppRouting
 ],
 ...
})

在这个简单的例子里我要对app.component.html里的内容进行animate,所以我的

app.component.ts

@Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
 styleUrls: ['./app.component.css'],
 animations: [] // 这里代码我省略了,先说一下结构,后面说具体实现。
})

以上就是需要写动画实现的基本结构,下面贴实现这个例子的代码。为了方便阅读,我把代码解释就贴在代码旁边

例一:

这是路由配置:

import {RouterModule, Routes} from '@angular/router';
import {HomeComponent} from "./home/home.component";
import {AboutComponent} from "./about/about.component";
const routes: Routes = [
 { path: '', redirectTo: 'home', pathMatch: 'full' },
 { path: 'home', component: HomeComponent, data: { state: 'home' } },
 { path: 'about', component: AboutComponent, data: { state: 'about' } }
];
export const AppRouting = RouterModule.forRoot(routes, {
 useHash: true
});

app.component.html

<nav>
 <a routerLink="home" routerLinkActive="active">Home</a>
 <a routerLink="about" routerLinkActive="active">About</a>
</nav>
<main [@routerTransition] = "gg(o)">
 <router-outlet #o="outlet"></router-outlet>
</main>
<div [@queryAnimation]="goAnimate()">
 <div class="content">
 Blah blah blah
 </div>
 <h1>Title</h1>
</div>
 <!--
 [@routerTransition]="gg(o)" ,api:transition declares the sequence of animation steps that will be run when the provided stateChangeExpr value is satisfied. stateChangeExpr即等号左边即动画名称,注意中括号和@符不能省略,等号右边是一个函数,也可以是变量,满足条件便可以让动画进行,一个动画可以多次使用
 -->

app.component.ts

import { Component } from '@angular/core';
import {routerTransition} from './router.animation';
import {animate, group, query, stagger, style, transition, trigger} from "@angular/animations";
@Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
 styleUrls: ['./app.component.css'],
 animations: [
 trigger('routerTransition', [ // 第一个参数是动画名称 stateChangeExpr
  transition('* <=> *', [ // 指定什么时候执行动画,状态的来源可以是简单的对象属性,也可以是由方法计算出来的值。重点是,我们得能从组件模板中读取它。官网上有提供一些通配符,[传送门](https://angular.cn/api/animations/transition)
  query(':enter, :leave', style({ position: 'fixed', width: '100%' }), { optional: true }),
  query('.block', style({ opacity: 0 }), { optional: true }),
  group([ // block executes in parallel
      query(':enter', [style({ transform: 'translateX(100%)' }),
      animate('0.5s ease-in-out', style({ transform: 'translateX(0%)' }))], { optional: true }),
      query(':leave', [style({ transform: 'translateX(0%)' }),
      animate('0.5s ease-in-out', style({ transform: 'translateX(-100%)' }))], { optional: true })
     ]),
   query(':enter .block', stagger(400, [style({ transform: 'translateY(100px)' }),
      animate('1s ease-in-out', style({ transform: 'translateY(0px)', opacity: 1 })),
    ]), { optional: true }),
  ])
 ]),
]
})
export class AppComponent {
 public exp = '';
 gg(outlet) { // 传递进入的组件的信息
 console.log(outlet.activatedRouteData.state);
 return outlet.activatedRouteData.state;
 }
}

效果动图在最后。

比对着官网给的API,总结一下动画部分~

我是按自己的理解说的,有不对的地方还请多多指教,共勉!O(∩_∩)O~

  • stateChangeExpr

即动画名称,它的属性值可以是字符串也可以是函数,若是函数,则每次状态发生改变都会重新执行,若函数返回true,则对应的动画就会执行。

  • transition

它里面的动画只在满足条件时执行,过了这个点它就变回原始样式了,

  • state

可以保持动画样式

  • :enter 、 :leave

即对应void => * 、 * => void 状态

例子二

app.component.html

<div [@queryAnimation]="goAnimate()">
 <h1>Title</h1>
 <div class="content">
 Blah blah blah
 </div>
</div>

app.component.ts

import { Component } from '@angular/core';
import {animate, group, query, stagger, state, style, transition, trigger} from "@angular/animations";
@Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
 styleUrls: ['./app.component.css'],
 animations: [
 trigger('queryAnimation', [
  transition('* => *', [
   query('h1', style({ opacity: 0 , color: 'red'})),
   query('.content', style({ opacity: 0, color: 'green', width: '100px', height: '100px', border: '1px solid red' })),
   query('h1', animate(1000, style({ opacity: 1, color: ' blue' }))),
   query('.content', animate(1000, style({ opacity: 1, width: '50px', height: '100px', border: '10px solid green'})),
    {optional: true}),
 ]),
  transition(':leave', [
  style({color: 'pink'}),
  animate(2000)
  ])
]),
]
})
export class AppComponent {
 public gg: string;
 constructor() {
 }
 goAnimate() {
 return true;
 }
}

这个gif有点卡,但是可以大概看出路由切换时是有动画的。这是上面两个例子的效果图

state只能放在trigger里,不能搁在transition里

目前就这么点,才看了一点,以后慢慢补充~~~

更多关于AngularJS相关内容感兴趣的读者可查看本站专题:《AngularJS指令操作技巧总结》、《AngularJS入门与进阶教程》及《AngularJS MVC架构总结》

希望本文所述对大家AngularJS程序设计有所帮助。

(0)

相关推荐

  • 在AngularJS应用中实现一些动画效果的代码

    在Angular当中,CSS和JavaScript之间唯一的区别就是它们的定义.没有什么区别妨碍到被定义的动画被使用.首先,我们需要加载ngAnimate模块到我们应用的root模块当中. angular.module('coursesApp', ['ngAnimate']); 而所有将被处理的JavaScript动画事件依然保持不变.以下是一个直接支持的动画列表和它们对应的不同行为: 指令事件集 ng-view ng-include ng-switch ng-if  enter leave n

  • AngularJS中实现动画效果的方法

    AngularJS 动画 AngularJS 提供了动画效果,可以配合 CSS 使用. AngularJS 使用动画需要引入 angular-animate.min.js 库. <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular-animate.min.js"></script> 还需在应用中使用模型 ngAnimate: <body ng-app="ngAnimate

  • 使用Angular 6创建各种动画效果的方法

    就技术角度而言,动画可以被定义为从初始状态到最终状态的转换过程.如今它已是各种Web应用不可或缺的组成部分.通过动画,我们不仅能创建出各种酷炫的UI,同时它们也能增加应用程序的趣味性.因此,设计精美的动画在吸引用户眼球的同时,也增强了他们的浏览体验. Angular能够让我们创建出具有原生表现效果的动画.我们将通过本文学习到如何使用Angular 6来创建各种动画效果.在此,我们将使用Visual Studio Code来进行示例演示. 准备工作 安装Visual Studio Code和Ang

  • 给angular加上动画效遇到的问题总结

    加入"动效"是让用户对应用的行为进行感知的一种有效手段."列表"是应用中最常使用的一种界面形式,经常会有添加行,删除行,移动行这些操作.设想添加的操作很简单,删除时从大到小,然后消失:添加时从小到大:移动就是先删除再添加.感觉上并不复杂,应该利用CSS的transition就能搞定,可是实际做起来发现有不少问题要处理,下面一一道来. 来些简单的测试 1.最初的版本 <div class='list'> <div class='row-1'>r

  • AngularJS 实现JavaScript 动画效果详解

    AngularJS 应用中实现 JavaScript 动画效果 AngularJS 是一组用于创建单页Web应用的丰富框架,给构建丰富交互地应用带来了所有需要的功能.其中一项主要的特性就是Angular带来了对动画的支持. 我们能够在应用的部分内容当中使用动画来表明一些变化正在发生.在我上一篇文章当中,我讲到了在Angular应用中对CSS动画的支持.在这篇文章当中,我们会看到怎样利用JavaScript脚本在AngularJS应用当中生成动画效果. 在Angular当中,CSS和JavaScr

  • 基于Angular.js实现的触摸滑动动画实例代码

    先上图: 这个主要用到是angular-touch.js中封装好的ng-swipe-left,ng-swipe-right,向左或向右的触摸事件.结合css3的transition实现的动画.ng-class为切换写好的动画的className. <!DOCTYPE HTML> <html ng-app="myapp"> <head> <meta http-equiv="content-type" content="

  • angular4 如何在全局设置路由跳转动画的方法

    最近用angular4写项目需要为每次路由跳转增加动画,看了一下官方文档,虽然可以实现,但是要每个组件都引入一次animations,比较麻烦,找网上也查阅了很多资料,但是都没找到适用的方法,最后自己写了一种方法如下: 首先在app.module中导入BrowserAnimationsModule import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; @NgModule({ imports:

  • Angular4如何自定义首屏的加载动画详解

    前言 相信大家都知道,在默认情况下,Angular应用程序在首次加载根组件时,会在浏览器的显示一个loading... 我们可以轻松地将loading修改成我们自己定义的动画.下面话不多说,来一起看看详细的介绍: 这是我们要实现首次加载的效果: 根组件标签中的内容 请注意:在你的入口文件index.html中,默认的loading...只是插入到根组件标签之间: <!doctype html> <html> <head> <meta charset="u

  • AngularJS入门之动画

    前言 AngularJS 提供了动画效果,可以配合 CSS 使用.AngularJS 使用动画需要引入 angular-animate.min.js 库. <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular-animate.min.js"></script> 还需在应用中使用模型 ngAnimate: <body ng-app="ngAnimate">

  • AngularJS中实现显示或隐藏动画效果的方式总结

    AngularJS 是一组用于创建单页Web应用的丰富框架,给构建丰富交互地应用带来了所有需要的功能.其中一项主要的特性就是Angular带来了对动画的支持. 本篇体验在AngularJS中实现在"显示/隐藏"这2种状态切换间添加动画效果. 通过CSS方式实现显示/隐藏动画效果 思路: →npm install angular-animage →依赖:var app = angular.module("app",["ngAnimate"]); →

随机推荐