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

网友投稿 321 2023-03-19


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

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

exCsmBTlTJZport 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.dihttp://splay') display = 'block';

@HostBinding('style.position') position = 'absolute';

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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:vue2 router 动态传参,多个参数的实例
下一篇:python api网关(openresty api网关)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~