需求:给项目管理页面加上搜索和重置的过场动画。
最先想到的就是利用angular2的animations属性。
// project.component.ts import {trigger, state, style, animate, transition} from '@angular/animations'; @Component({ selector: 'projects', template: require('./projects.html'), styleUrls: ['./projects.css'], providers: [ProjectService], animations: [ trigger('projectIn', [ state('active', style({transform: 'translateX(0)', opacity: 1})), transition('void => *', [ style({transform: 'translateX(500px)', opacity: 0}), animate('1s ease-in-out') ]) ]), ] }) export class ProjectComponent{ state: tring = 'active'; } // project.component.ts import {trigger, state, style, animate, transition} from '@angular/animations'; @Component({ selector: 'projects', template: require('./projects.html'), styleUrls: ['./projects.css'], providers: [ProjectService], animations: [ trigger('projectIn', [ state('active', style({transform: 'translateX(0)', opacity: 1})), transition('void => *', [ style({transform: 'translateX(500px)', opacity: 0}), animate('1s ease-in-out') ]) ]), ] }) export class ProjectComponent{ state: tring = 'active'; }
将动画绑定在HTML模板上
<tr *ngFor="let project of projects" [@projectIn]="state"> <tr *ngFor="let project of projects" [@projectIn]="state">
给重置按钮和搜索按钮也来个旋转的特效吧。
最简单的方案就是利用项目中的bootstrap库,在搜索或者重置时改变按钮内部的i标签;
首先改造HTML模板;
<button type="button" class="btn searchbtn btn-primary"(click)="search(); getProjects(pagecount.value, 1, projectName.value)"><i [ngClass]='searchClass'>{{searchValue}}</i></button> // search 按钮 <button (click)="reset(); getProjects();projectName.value = '';" type="button" class="btn btn-primary"><i [ngClass] = "resetClass"></i></button> // reset 按钮 <button type="button" class="btn searchbtn btn-primary"(click)="search(); getProjects(pagecount.value, 1, projectName.value)"><i [ngClass]='searchClass'>{{searchValue}}</i></button> // search 按钮 <button (click)="reset(); getProjects();projectName.value = '';" type="button" class="btn btn-primary"><i [ngClass] = "resetClass"></i></button> // reset 按钮
改造ts文件
resetClass: string = 'fa fa-repeat'; searchClass: string = ''; searchValue: string = '搜索'; reset() { this.resetClass = 'fa fa-repeat fa-spin'; setTimeout(() => this.resetClass = "fa fa-repeat", 2000); } search() { this.searchValue = ''; this.searchClass = 'fa fa-repeat fa-spin'; setTimeout(() => { this.searchClass = ''; this.searchValue = '搜索'; }, 2000) } resetClass: string = 'fa fa-repeat'; searchClass: string = ''; searchValue: string = '搜索'; reset() { this.resetClass = 'fa fa-repeat fa-spin'; setTimeout(() => this.resetClass = "fa fa-repeat", 2000); } search() { this.searchValue = ''; this.searchClass = 'fa fa-repeat fa-spin'; setTimeout(() => { this.searchClass = ''; this.searchValue = '搜索'; }, 2000) }
原理简单粗暴 即点击触发函数改变CSS值,2秒后恢复原有CSS值。。
如果你想再加个弹窗的话可以利用现成的swalert库;
// 直接在getprojects里面加上如下代码 swal({ title: 'loading', type: 'success', timer: 1000, showConfirmButton: false, }).catch(()=>{}); //即每次获取数据后触发弹窗动画。 // 直接在getprojects里面加上如下代码 swal({ title: 'loading', type: 'success', timer: 1000, showConfirmButton: false, }).catch(()=>{}); //即每次获取数据后触发弹窗动画。
基本效果已经实现了,现在把效果复制到每个组件去
Excuse me"text-align: center">
// app.module.ts 添加如下代码
import {QbcSearchComponent} from './component/qbc-search/qbc-search.component'; import {QbcResetComponent} from './component/qbc-reset/qbc-reset.component'; declarations: [ QbcSearchComponent,QbcResetComponent]
// app.module.ts 添加如下代码
import {QbcSearchComponent} from './component/qbc-search/qbc-search.component'; import {QbcResetComponent} from './component/qbc-reset/qbc-reset.component'; declarations: [ QbcSearchComponent,QbcResetComponent] //qbc-search.component.ts 添加如下代码 import { Component, Output, EventEmitter} from '@angular/core'; import swal from 'sweetalert2'; @Component({ selector: 'qbc-search', template: require('./qbc-search.html'), }) export class QbcSearchComponent { @Output() searchEmitter = new EventEmitter(); searchClass: string = ''; searchValue: string = '搜索'; constructor() {} search(value) { this.searchValue = ''; this.searchClass = 'fa fa-repeat fa-spin'; setTimeout(() => { this.searchClass = ''; this.searchValue = '搜索'; }, 2000) this.searchEmitter.emit(value); swal({ title: 'loading', type: 'success', timer: 1000, showConfirmButton: false, }).catch(()=>{}); } } //qbc-search.component.ts 添加如下代码 import { Component, Output, EventEmitter} from '@angular/core'; import swal from 'sweetalert2'; @Component({ selector: 'qbc-search', template: require('./qbc-search.html'), }) export class QbcSearchComponent { @Output() searchEmitter = new EventEmitter(); searchClass: string = ''; searchValue: string = '搜索'; constructor() {} search(value) { this.searchValue = ''; this.searchClass = 'fa fa-repeat fa-spin'; setTimeout(() => { this.searchClass = ''; this.searchValue = '搜索'; }, 2000) this.searchEmitter.emit(value); swal({ title: 'loading', type: 'success', timer: 1000, showConfirmButton: false, }).catch(()=>{}); } } //qbc-search.html <div class="input-group"> <input type="text" placeholder="请输入名称" class="searchinput form-control" #name> <span class="input-group-btn"><button type="button" class="btn searchbtn btn-primary" (click)="search(name.value);"><i [ngClass]='searchClass'>{{searchValue}}</i></button></span> </div> //qbc-search.html <div class="input-group"> <input type="text" placeholder="请输入名称" class="searchinput form-control" #name> <span class="input-group-btn"><button type="button" class="btn searchbtn btn-primary" (click)="search(name.value);"><i [ngClass]='searchClass'>{{searchValue}}</i></button></span> </div>
接下来需要改写项目HTML
//projects.html //将原先的搜索框代码部分用qbc-search代替。 <qbc-search (searchEmitter)=search(pagecount.value,1,$event)></qbc-search> //projects.html //将原先的搜索框代码部分用qbc-search代替。 <qbc-search (searchEmitter)=search(pagecount.value,1,$event)></qbc-search>
然后是项目TS文件
//projects.component.ts // 其实也可以直接在模板中调用getProjects方法,差不多。一个是后期要修改模板,一个是要修改TS文件。 search(pageSize, page, name) { this.getProjects(pageSize, page, name); } //projects.component.ts // 其实也可以直接在模板中调用getProjects方法,差不多。一个是后期要修改模板,一个是要修改TS文件。 search(pageSize, page, name) { this.getProjects(pageSize, page, name); }
qbc-reset实现方式雷同就不赘述了。下面看看animations如何复用。
// 先试试可不可以放入app.component.ts animations: [ trigger('fadeIn', [ state('active', style({transform: 'translateX(0)', opacity: 1})), transition('void => *', [ style({transform: 'translateX(500px)', opacity: 0}), animate('1s ease-in-out') ]) ]), ] // 先试试可不可以放入app.component.ts animations: [ trigger('fadeIn', [ state('active', style({transform: 'translateX(0)', opacity: 1})), transition('void => *', [ style({transform: 'translateX(500px)', opacity: 0}), animate('1s ease-in-out') ]) ]), ] //projects.html [@fadeIn] = "state" // error The provided animation trigger "c1#fadeIn" has not been registered! //projects.html [@fadeIn] = "state" // error The provided animation trigger "c1#fadeIn" has not been registered!
看来这种方式不行,在没弄清楚angular2动画全局复用机制前,我们先用原生CSS代替。
建立animation.css
.fadeIn{ animation: fadeIn ease-in-out 1.5s 1; // 参数依次为: 动画名称 缓动函数 动画时间 动画运行次数 } @keyframes fadeIn{ 0% { opacity: 0; transform: translateX(500px); } 100%{ opacity: 1; transform: translateX(0); } } .fadeIn{ animation: fadeIn ease-in-out 1.5s 1; // 参数依次为: 动画名称 缓动函数 动画时间 动画运行次数 } @keyframes fadeIn{ 0% { opacity: 0; transform: translateX(500px); } 100%{ opacity: 1; transform: translateX(0); } }
直接在项目里引用CSS文件,并在模板里面添加class名fadeIn;
//projects.component.ts styleUrls: ['./projects.css', '../animation.css'] //projects.html <tr *ngFor="let project of projects" class="fadeIn"> //projects.component.ts styleUrls: ['./projects.css', '../animation.css'] //projects.html <tr *ngFor="let project of projects" class="fadeIn">
实现效果如下
老铁还有没有更简单的,我不会CSS3,别跟我整那些幺蛾子。
当然有!!!
// projects.html // bootstrap库帮你写好了,填写class就好 <tr *ngFor="let project of projects" class="animated fadeInRight"> // projects.html // bootstrap库帮你写好了,填写class就好 <tr *ngFor="let project of projects" class="animated fadeInRight">
以上所述是小编给大家介绍的Angular2搜索和重置按钮过场动画,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月15日
2024年11月15日
- 丁红《女儿情》[DTS-WAV]
- 陈惠婷.2017-Voyager3【亚神音乐】【FLAC分轨】
- 群星.2001-华纳歌声魅影【华纳】【WAV+CUE】
- 张雨生.2016-雨后星空2CD【丰华】【WAV+CUE】
- 李雨寰《Break Free》[FLAC/分轨][533MB]
- 廖也欧《面朝大海》[320K/MP3][18.35MB]
- 廖也欧《面朝大海》[Hi-Res][24bit 48kHz][FLAC/分轨][170.14MB]
- s13T1夺冠五人名单都有谁 s13T1夺冠五人名单一览
- 英雄联盟T1战队队长都有谁 T1战队所有队长介绍
- skt历届战队成员都有哪些 skt历届战队成员名单盘点
- 妙音唱片《大热唱片3》[WAV+CUE]
- 费玉清《跟着地球旋转》滚石时代经典复刻[正版原抓WAV+CUE]
- 罗文甄妮-射雕英雄传(AMCD)(限量版)[WAV+CUE]
- 《巫师4》定档2025再添佐证:参演人员曝光 老将回归
- 辣眼睛 美女COS《黑神话:悟空》比基尼版金池长老