iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Angular路由动画及高阶动画函数的示例分析
  • 328
分享到

Angular路由动画及高阶动画函数的示例分析

2023-06-15 04:06:10 328人浏览 泡泡鱼
摘要

小编给大家分享一下angular路由动画及高阶动画函数的示例分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!一、路由动画路由动画需要在host元数据中指定触发器。动画注意不要过多,否则适得其反。内容优先,引导用户去注意到

小编给大家分享一下angular路由动画及高阶动画函数的示例分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

一、路由动画

路由动画需要在host元数据中指定触发器。动画注意不要过多,否则适得其反。

内容优先,引导用户去注意到某个内容。动画只是辅助手段。

在router.animate.ts中定义一个进场动画,一个离场动画。

因为进场动画和离场动画用的特别频繁,有一个别名叫:enter和:leave。

import { trigger, state, transition, style, animate} from '@angular/animations';export const slideToRight = trigger('routeAnim',[    state('void',style({'position':'fixed','width':'100%','height':'100%'})),    state('*',style({'position':'fixed','width':'100%','height':'80%'})),    transition('void => *',[        style({transfORM:'translateX(-100%)'}),        animate('.5s ease-in-out', style({transform:'translateX(0)'}))    ]),    transition('* => void',[        style({transform:'translateX(0)'}),        animate('.5s ease-in-out', style({transform:'translateX(100%)'}))    ]),]);

在project-list中使用路由动画。

import { Component, OnInit , HostBinding } from "@angular/core";import { MatDialog } from "@angular/material";import { NewProjectComponent } from "../new-project/new-project.component";import { InviteComponent } from '../invite/invite.component';import { ConfirmDialoGComponent } from '../../shared/confirm-dialog/confirm-dialog.component';import {slideToRight} from '../../animate/router.animate'@Component({  selector: "app-project-list",  templateUrl: "./project-list.component.html",  styleUrls: ["./project-list.component.sCSS"],  animations:[    slideToRight  ]})export class ProjectListComponent implements OnInit {  @HostBinding('@routeAnim') state;  projects = [    {      name: "企业协作平台",      desc: "这是一个企业内部项目",      coverImg: "assets/images/covers/0.jpg"    },    {      name: "自动化测试项目",      desc: "这是一个企业内部项目",      coverImg: "assets/images/covers/2.jpg"    }  ];  constructor(private dialog: MatDialog) { }  nGonInit() { }  openNewProjectDialog() {    // this.dialog.open(NewProjectComponent,{data:'this is a dialog'});    const dialogRef = this.dialog.open(NewProjectComponent, {      data: { title: '新建项目' }    });    dialogRef.afterClosed().subscribe((result) => {      console.log(result);    });  }  lauchInviteDialog() {    const dialogRef = this.dialog.open(InviteComponent);  }  lauchUpdateDialog() {    const dialogRef = this.dialog.open(NewProjectComponent, {      data: { title: '编辑项目' }    });  }  lauchConfimDialog() {    const dialogRef = this.dialog.open(ConfirmDialogComponent, {      data: { title: '编辑项目', content: '您确认删除该项目吗?' }    });  }}

在task-home中使用路由动画。

import { Component, OnInit , HostBinding } from "@angular/core";import { NewTaskComponent } from "../new-task/new-task.component";import { MatDialog } from "@angular/material";import { CopyTaskComponent } from "../copy-task/copy-task.component";import { ConfirmDialogComponent } from "../../shared/confirm-dialog/confirm-dialog.component";import { NewTaskListComponent } from "../new-task-list/new-task-list.component";import {slideToRight} from '../../animate/router.animate';@Component({  selector: "app-task-home",  templateUrl: "./task-home.component.html",  styleUrls: ["./task-home.component.scss"],  animations:[    slideToRight  ]})export class TaskHomeComponent implements OnInit {  constructor(private dialog: MatDialog) {}  @HostBinding('@routeAnim') state;  ngOnInit() {}  launchNewTaskDialog() {    // this.dialog.open(NewTaskComponent);    const dialogRef = this.dialog.open(NewTaskComponent, {      data: { title: "新建任务" }    });  }  lauchCopyTaskDialog() {    const dialogRef = this.dialog.open(CopyTaskComponent, {      data: { lists: this.lists }    });  }  launchUpdateTaskDialog(task) {    const dialogRef = this.dialog.open(NewTaskComponent, {      data: { title: "修改任务", task: task }    });  }  launchConfirmDialog() {    const dialogRef = this.dialog.open(ConfirmDialogComponent, {      data: { title: "删除任务列表", content: "您确定要删除该任务列表吗?" }    });  }  launchEditListDialog() {    const dialogRef = this.dialog.open(NewTaskListComponent, {      data: { title: "更改列表名称" }    });    dialogRef.afterClosed().subscribe(result => console.log(result));  }  launchNewListDialog() {    const dialogRef = this.dialog.open(NewTaskListComponent, {      data: { title: "新建列表名称" }    });    dialogRef.afterClosed().subscribe(result => console.log(result));  }  lists = [    {      id: 1,      name: "待办",      tasks: [        {          id: 1,          desc: "任务一: 去星巴克买咖啡",          completed: true,          priority: 3,          owner: {            id: 1,            name: "张三",            avatar: "avatars:svg-11"          },          dueDate: new Date(),          reminder: new Date()        },        {          id: 2,          desc: "任务一: 完成老板布置的PPT作业",          completed: false,          priority: 2,          owner: {            id: 2,            name: "李四",            avatar: "avatars:svg-12"          },          dueDate: new Date()        }      ]    },    {      id: 2,      name: "进行中",      tasks: [        {          id: 1,          desc: "任务三: 项目代码评审",          completed: false,          priority: 1,          owner: {            id: 1,            name: "王五",            avatar: "avatars:svg-13"          },          dueDate: new Date()        },        {          id: 2,          desc: "任务一: 制定项目计划",          completed: false,          priority: 2,          owner: {            id: 2,            name: "李四",            avatar: "avatars:svg-12"          },          dueDate: new Date()        }      ]    }  ];}

定义路由

<mat-list-item [routerLink]="['/project']">     <mat-icon mat-list-icon svgIcon="projects"></mat-icon>    <h5 mat-line>项目首页</h5>    <p mat-line mat-subheader> 查看您的所有项目</p>  </mat-list-item>  <mat-list-item [routerLink]="['/task']">     <mat-icon mat-list-icon svgIcon="projects"></mat-icon>    <h5 mat-line>任务首页</h5>    <p mat-line mat-subheader> 查看您的所有项目</p>  </mat-list-item>

注意:一定要用HostBinding形式。

二、Group

用于同时进行一组动画变换

group([animate(...),animate(...)...])接收一个数组,数组里写多个动画。

import { trigger, state, transition, style, animate, group} from '@angular/animations';export const slideToRight = trigger('routeAnim',[    state('void',style({'position':'fixed','width':'100%','height':'80%'})),    state('*',style({'position':'fixed','width':'100%','height':'80%'})),    transition(':enter',[        style({transform:'translateX(-100%)',opacity:'0'}),        group([            animate('.5s ease-in-out', style({transform:'translateX(0)'})),            animate('.3s ease-in', style({opacity:1}))        ])    ]),    transition(':leave',[        style({transform:'translateX(0)',opacity:'1'}),        group([            animate('.5s ease-in-out', style({transform:'translateX(100%)'})),            animate('.3s ease-in', style({opacity:0}))        ])    ]),]);

三、Query & Stagger

Query用于父节点寻找子节点,把动画应用到选中元素。非常强大。

Stagger指定有多个满足Query的元素,每个的动画之间有间隔。

做一个示例:新建的时候同时新建2个项目,两个新建出的项目的动画依次产生,第一个完成后才开始第二个。

Angular路由动画及高阶动画函数的示例分析

建立list.animate.ts

进场动画,先隐藏起来,通过stagger间隔1000s做一个1s的动画。

import { trigger, state, transition, style, animate, query, animation,stagger} from '@angular/animations';export const listAnimation = trigger('listAnim', [    transition('* => *', [      query(':enter', style({opacity: 0}), { optional: true }), //加入optional为true,后面的状态动画都是可选的      query(':enter', stagger(1000, [        animate('1s', style({opacity: 1}))      ]), { optional: true }),      query(':leave', style({opacity: 1}), { optional: true }),      query(':leave', stagger(1000, [        animate('1s', style({opacity: 0}))      ]), { optional: true })    ])  ]);

在project_list中使用

应用query动画一般都是跟*ngFor在一起的,需要外面套一层div。

<div class="container" [@listAnim]="projects.length">  <app-project-item *ngFor="let project of projects" [item]="project"  class="card"  (onInvite)="lauchInviteDialog()"  (onEdit)="lauchUpdateDialog()"  (onDelete)="lauchConfimDialog(project)">  </app-project-item></div><button class="ab-buttonmad-fab fab-button" mat-fab type="button" (click)="openNewProjectDialog()">  <mat-icon>add</mat-icon></button>

修改对应的css

// :host{//     display: flex;//     flex-direction: row;//     flex-wrap: wrap;// }//把host改为div.container{    display: flex;    flex-direction: row;    flex-wrap: wrap;}

修改一下component

import { Component, OnInit , HostBinding } from "@angular/core";import { MatDialog } from "@angular/material";import { NewProjectComponent } from "../new-project/new-project.component";import { InviteComponent } from '../invite/invite.component';import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.component';import {slideToRight} from '../../animate/router.animate'import { listAnimation } from '../../animate/list.animate';import { projection } from '@angular/core/src/render3';@Component({  selector: "app-project-list",  templateUrl: "./project-list.component.html",  styleUrls: ["./project-list.component.scss"],  animations:[    slideToRight,listAnimation  //第一步,导入listAnimation  ]})export class ProjectListComponent implements OnInit {  @HostBinding('@routeAnim') state;  //第二步,改造一下数组,加id  projects = [    {      id:1,      name: "企业协作平台",      desc: "这是一个企业内部项目",      coverImg: "assets/images/covers/0.jpg"    },    {      id:2,      name: "自动化测试项目",      desc: "这是一个企业内部项目",      coverImg: "assets/images/covers/2.jpg"    }  ];  constructor(private dialog: MatDialog) { }  ngOnInit() { }  //第三步,新增元素时hard code一下  openNewProjectDialog() {    // this.dialog.open(NewProjectComponent,{data:'this is a dialog'});    const dialogRef = this.dialog.open(NewProjectComponent, {      data: { title: '新建项目' }    });    dialogRef.afterClosed().subscribe((result) => {      console.log(result);      this.projects = [...this.projects,         {id:3,name:'一个新项目',desc:'这是一个新项目',coverImg:"assets/images/covers/3.jpg"},        {id:4,name:'又一个新项目',desc:'这是又一个新项目',coverImg:"assets/images/covers/4.jpg"}]    });  }  lauchInviteDialog() {    const dialogRef = this.dialog.open(InviteComponent);  }  lauchUpdateDialog() {    const dialogRef = this.dialog.open(NewProjectComponent, {      data: { title: '编辑项目' }    });  }  //第四步,改造一下删除项目  lauchConfimDialog(project) {    const dialogRef = this.dialog.open(ConfirmDialogComponent, {      data: { title: '删除项目', content: '您确认删除该项目吗?' }    });    dialogRef.afterClosed().subscribe(result=>{      console.log(result);      this.projects=this.projects.filter(p=>p.id!=project.id);    });  }}

Stagger使得在多个元素时候,动画交错开,而不是一起。

看完了这篇文章,相信你对“Angular路由动画及高阶动画函数的示例分析”有了一定的了解,如果想了解更多相关知识,欢迎关注编程网精选频道,感谢各位的阅读!

--结束END--

本文标题: Angular路由动画及高阶动画函数的示例分析

本文链接: https://www.lsjlt.com/news/277580.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Angular路由动画及高阶动画函数的示例分析
    小编给大家分享一下Angular路由动画及高阶动画函数的示例分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!一、路由动画路由动画需要在host元数据中指定触发器。动画注意不要过多,否则适得其反。内容优先,引导用户去注意到...
    99+
    2023-06-15
  • 详解Angular路由动画及高阶动画函数
    目录一、路由动画二、Group三、Query & Stagger一、路由动画 路由动画需要在host元数据中指定触发器。动画注意不要过多,否则适得其反。 内容优先,引导用户去...
    99+
    2024-04-02
  • JavaScript动画函数封装的示例分析
    小编给大家分享一下JavaScript动画函数封装的示例分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!一、动画函数原理核心原理:通过定时器setInterval() 不断移动盒子位置。实现步骤:获得盒子当前位置让盒子在...
    99+
    2023-06-22
  • css动画的示例分析
    这篇文章主要为大家展示了“css动画的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“css动画的示例分析”这篇文章吧。   CSS3动画是什么?  ...
    99+
    2024-04-02
  • css中运动路径动画Motion Path的示例分析
    这篇文章给大家介绍css中运动路径动画Motion Path的示例分析,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。CSS 中有一个非常有意思的模块 -- CSS Motion Pat...
    99+
    2024-04-02
  • Android动画之小球拟合动画的示例分析
    这篇文章给大家分享的是有关Android动画之小球拟合动画的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。Android动画之小球拟合动画实例实现效果:动画组成:通过三阶贝塞尔曲线来拟合圆,拟合系数的由来...
    99+
    2023-05-31
    android
  • css3的动画特效之动画序列的示例分析
    这篇文章给大家分享的是有关css3的动画特效之动画序列的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。首先复习一下animation动画添加各种参数(1)infinite...
    99+
    2024-04-02
  • angular路由之angular-router的示例分析
    这篇文章主要介绍了angular路由之angular-router的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。具体如下:创建项目...
    99+
    2024-04-02
  • Angular中路由的示例分析
    这篇文章将为大家详细讲解有关Angular中路由的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。1. 摘要简单来说地址栏中,不同的地址(URL)对应不同的页面,这...
    99+
    2024-04-02
  • Angular4.0动画操作的示例分析
    这篇文章主要为大家展示了“Angular4.0动画操作的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Angular4.0动画操作的示例分析”这篇文章吧...
    99+
    2024-04-02
  • Vue3过渡动画的示例分析
    这期内容当中小编将会给大家带来有关Vue3过渡动画的示例分析,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。背景在我的 《Vue 3 开发企业级音乐 App》课程问答区,有个同学提了个问题,在歌手列表到歌手...
    99+
    2023-06-22
  • Bootstrap过渡动画的示例分析
    这篇文章主要为大家展示了“Bootstrap过渡动画的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Bootstrap过渡动画的示例分析”这篇文章吧。动...
    99+
    2024-04-02
  • loading动画特效的示例分析
    这篇文章给大家分享的是有关loading动画特效的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。代码示例<!DOCTYPE html> <ht...
    99+
    2024-04-02
  • vue动态路由配置及路由传参的示例分析
    这篇文章主要介绍了vue动态路由配置及路由传参的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。动态路由:  当我们很多个页面或者组件...
    99+
    2024-04-02
  • Angular中Route路由的示例分析
    这篇文章主要介绍Angular中Route路由的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!Angular 路由(Route)我们可以将路由器理解成控制整个应用状态的视图对象, 每个应用都有一个路由器; 路...
    99+
    2023-06-14
  • Angular 4.x路由的示例分析
    这篇文章给大家分享的是有关Angular 4.x路由的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。Installing the router首先第一件事,我们需要安装 ...
    99+
    2024-04-02
  • vue3中过渡动画的示例分析
    这篇文章主要介绍了vue3中过渡动画的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。一、vue中动画简单介绍在vue中如果一些过程不存在动画效果,则表现出来的结果是比...
    99+
    2023-06-29
  • jquery中animate动画持续运动的示例分析
    这篇文章给大家分享的是有关jquery中animate动画持续运动的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。如下所示:function fingers()...
    99+
    2024-04-02
  • Angular中路由和表单的示例分析
    这篇文章主要介绍Angular中路由和表单的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!Angular的路由介绍在单页面应用中,需要在定义好的不同视图中(组件)来回切换,而...
    99+
    2024-04-02
  • Angular路由复用策略的示例分析
    这篇文章主要介绍了Angular路由复用策略的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。一、引言路由在执行过程中对组件无状态操作...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作