当前位置: 移动技术网 > IT编程>脚本编程>AngularJs > angular2系列之路由转场动画的示例代码

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

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';

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网