【Angular前端工程化】——angular2实现页面动画效果

前端之家收集整理的这篇文章主要介绍了【Angular前端工程化】——angular2实现页面动画效果前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

【前言】
在前端的路上越走越远,也希望利用angular2使自己的页面效果更加美观,所以从官网上学习了一下
内容
1.写animation.ts

import { animate,AnimationEntryMetadata,state,style,transition,trigger } from '@angular/core';


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%)'
      }))
    ])
  ]);

2.在使用效果的组件ts中引入,我定义了一个demo.ts

import 'rxjs/add/operator/switchMap';
import { Component,OnInit,HostBinding } from '@angular/core';
import { Router,ActivatedRoute,ParamMap } from '@angular/router';

import { slideInDownAnimation } from '../animations';  //引入滑动页面效果

import { Hero,HeroService }  from './hero.service';

@Component({
  template: `
  <h2>HEROES</h2>
  <div *ngIf="hero">
    <h3>"{{ hero.name }}"</h3>
    <div>
      <label>Id: </label>{{ hero.id }}</div>
    <div>
      <label>Name: </label>
      <input [(ngModel)]="hero.name" placeholder="name"/>
    </div>
    <p>
      <button (click)="gotoHeroes()">Back</button>
    </p>
  </div>
  `,animations: [ slideInDownAnimation ]
})
export class HeroDetailComponent implements OnInit {
  @HostBinding('@routeAnimation') routeAnimation = true;
  @HostBinding('style.display')   display = 'block';
  @HostBinding('style.position')  position = 'absolute';

  hero: Hero;

  constructor(
    private route: ActivatedRoute,private router: Router,private service: HeroService
  ) {}

  ngOnInit() {
    this.route.paramMap
      .switchMap((params: ParamMap) =>
        this.service.getHero(params.get('id')))
      .subscribe((hero: Hero) => this.hero = hero);
  }

  gotoHeroes() {
    let heroId = this.hero ? this.hero.id : null;
    this.router.navigate(['/heroes',{ id: heroId,foo: 'foo' }]);
  }
}

【总结】 主要就是在需要引入滑动的组件中引入这个效果,就达到了预期的成效!希望对你有帮助!

猜你在找的Angularjs相关文章