加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 09:05:07 所属栏目:安全 来源:网络整理
导读:【前言】 在前端的路上越走越远,也希望利用angular2使自己的页面效果更加美观,所以从官网上学习了一下 【内容】 1.写animation.ts import { animate,AnimationEntryMetadata,state,style,transition,trigger } from '@angular/core' ;export const slideIn

【前言】
在前端的路上越走越远,也希望利用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' }]);
  }
}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读