angular2系列之路由转场动画的示例代码

发布时间:2020-10-10 05:00:05 作者:steryn
来源:脚本之家 阅读:133

Angular2的动画系统赋予了制作各种动画效果的能力,致力于构建出与原生CSS动画性能相同的动画。

Angular2的动画主要是和@Component结合在了一起。

animations元数据属性在定义@Component装饰。就像template元数据属性!这样就可以让动画逻辑与其应用代码紧紧集成在一起,这让动画可以更容易的出发与控制。

一.在app.mudule.ts中引入:

import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

并在@NgModule中的imports添加:

imports: [BrowserAnimationsModule],

二.创建文件定义名为animations.ts用来书写转场动画

import { animate, AnimationEntryMetadata, state, style, transition, trigger } from'@angular/core';
// Component transition animations
export const slideInDownAnimation: AnimationEntryMetadata =
// 动画触发器名称
trigger('routeAnimation', [
  state('*',
    style({
      opacity: 1,
      transform: 'translateX(0)'
    })
  ),
  transition(':enter', [
    style({
      opacity: 0,
      transform: 'translateX(-100%)'
    }),
    animate('0.2s ease-in')
  ]),
  transition(':leave', [
    animate('0.5s ease-out', style({
      opacity: 0,
      transform: 'translateY(100%)'
    }))
  ])
]);

三.在需要添加转场动画的页面操作

引入import {HostBinding } from '@angular/core';(如果引入过直接将HostBinding添加进去就好,不要重复引入,多嘴了...)

再引入你写好的动画模板:import { slideInDownAnimation } from '../animation';

在@Component中添加:animations:[slideInDownAnimation],

最后:

  // 添加@HostBinding属性添加到类中以设置这个路由组件元素的动画和样式
  @HostBinding('@routeAnimation') routeAnimation = true;
  @HostBinding('style.display') display = 'block';
  @HostBinding('style.position') position = 'absolute';

四.至此你可以去浏览器看看效果了,如果没有错误

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

推荐阅读:
  1. 路由Routes之策略路由-从零开始学RouterOS系列1
  2. 路由Routes之静态路由-从零开始学RouterOS系列1

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

angular 转场动画 画的

上一篇:python Kmeans算法原理深入解析

下一篇:mysql数据库字符集学习总结

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》