Angular2搜索和重置按鈕過場(chǎng)動(dòng)畫
需求:給項(xiàng)目管理頁(yè)面加上搜索和重置的過場(chǎng)動(dòng)畫。
最先想到的就是利用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';
}
將動(dòng)畫綁定在HTML模板上
<tr *ngFor="let project of projects" [@projectIn]="state"> <tr *ngFor="let project of projects" [@projectIn]="state">
給重置按鈕和搜索按鈕也來個(gè)旋轉(zhuǎn)的特效吧。
最簡(jiǎn)單的方案就是利用項(xiàng)目中的bootstrap庫(kù),在搜索或者重置時(shí)改變按鈕內(nèi)部的i標(biāo)簽;
首先改造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)
}
原理簡(jiǎn)單粗暴 即點(diǎn)擊觸發(fā)函數(shù)改變CSS值,2秒后恢復(fù)原有CSS值。。
如果你想再加個(gè)彈窗的話可以利用現(xiàn)成的swalert庫(kù);
// 直接在getprojects里面加上如下代碼
swal({
title: 'loading',
type: 'success',
timer: 1000,
showConfirmButton: false,
}).catch(()=>{});
//即每次獲取數(shù)據(jù)后觸發(fā)彈窗動(dòng)畫。
// 直接在getprojects里面加上如下代碼
swal({
title: 'loading',
type: 'success',
timer: 1000,
showConfirmButton: false,
}).catch(()=>{});
//即每次獲取數(shù)據(jù)后觸發(fā)彈窗動(dòng)畫。
基本效果已經(jīng)實(shí)現(xiàn)了,現(xiàn)在把效果復(fù)制到每個(gè)組件去
Excuse me???
既然要復(fù)用,那就把搜索框和重置按鈕抽象成組件吧。
新建目錄如下

// 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="請(qǐng)輸入名稱" 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="請(qǐng)輸入名稱" 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>
接下來需要改寫項(xiàng)目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>
然后是項(xiàng)目TS文件
//projects.component.ts
// 其實(shí)也可以直接在模板中調(diào)用getProjects方法,差不多。一個(gè)是后期要修改模板,一個(gè)是要修改TS文件。
search(pageSize, page, name) {
this.getProjects(pageSize, page, name);
}
//projects.component.ts
// 其實(shí)也可以直接在模板中調(diào)用getProjects方法,差不多。一個(gè)是后期要修改模板,一個(gè)是要修改TS文件。
search(pageSize, page, name) {
this.getProjects(pageSize, page, name);
}
qbc-reset實(shí)現(xiàn)方式雷同就不贅述了。下面看看animations如何復(fù)用。
// 先試試可不可以放入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動(dòng)畫全局復(fù)用機(jī)制前,我們先用原生CSS代替。
建立animation.css
.fadeIn{
animation: fadeIn ease-in-out 1.5s 1; // 參數(shù)依次為: 動(dòng)畫名稱 緩動(dòng)函數(shù) 動(dòng)畫時(shí)間 動(dòng)畫運(yùn)行次數(shù)
}
@keyframes fadeIn{
0% {
opacity: 0;
transform: translateX(500px);
}
100%{
opacity: 1;
transform: translateX(0);
}
}
.fadeIn{
animation: fadeIn ease-in-out 1.5s 1; // 參數(shù)依次為: 動(dòng)畫名稱 緩動(dòng)函數(shù) 動(dòng)畫時(shí)間 動(dòng)畫運(yùn)行次數(shù)
}
@keyframes fadeIn{
0% {
opacity: 0;
transform: translateX(500px);
}
100%{
opacity: 1;
transform: translateX(0);
}
}
直接在項(xiàng)目里引用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">
實(shí)現(xiàn)效果如下

老鐵還有沒有更簡(jiǎn)單的,我不會(huì)CSS3,別跟我整那些幺蛾子。
當(dāng)然有?。?!
// projects.html // bootstrap庫(kù)幫你寫好了,填寫class就好 <tr *ngFor="let project of projects" class="animated fadeInRight"> // projects.html // bootstrap庫(kù)幫你寫好了,填寫class就好 <tr *ngFor="let project of projects" class="animated fadeInRight">
以上所述是小編給大家介紹的Angular2搜索和重置按鈕過場(chǎng)動(dòng)畫,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Angular4.0動(dòng)畫操作實(shí)例詳解
- Angular4如何自定義首屏的加載動(dòng)畫詳解
- 基于Angular.js實(shí)現(xiàn)的觸摸滑動(dòng)動(dòng)畫實(shí)例代碼
- 給angular加上動(dòng)畫效遇到的問題總結(jié)
- 利用CSS3在Angular中實(shí)現(xiàn)動(dòng)畫
- AngularJS中實(shí)現(xiàn)顯示或隱藏動(dòng)畫效果的方式總結(jié)
- 使用ngView配合AngularJS應(yīng)用實(shí)現(xiàn)動(dòng)畫效果的方法
- 在AngularJS應(yīng)用中實(shí)現(xiàn)一些動(dòng)畫效果的代碼
- 詳解Angular路由動(dòng)畫及高階動(dòng)畫函數(shù)
相關(guān)文章
淺談angularjs $http提交數(shù)據(jù)探索
這篇文章主要介紹了淺談angularjs $http提交數(shù)據(jù)探索,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01
使用Angular內(nèi)置模塊進(jìn)行HTTP請(qǐng)求
這篇文章主要介紹了使用Angular內(nèi)置模塊進(jìn)行HTTP請(qǐng)求方法步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
angular6的table組件開發(fā)的實(shí)現(xiàn)示例
這篇文章主要介紹了angular6的table組件開發(fā)的實(shí)現(xiàn)示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
Angular應(yīng)用程序的Hydration基本概念詳解
這篇文章主要為大家介紹了Angular應(yīng)用程序的Hydration基本概念詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
AngularJS學(xué)習(xí)第一篇 AngularJS基礎(chǔ)知識(shí)
這篇文章主要介紹了AngularJS學(xué)習(xí)第一篇,分享了有關(guān)AngularJS的基礎(chǔ)知識(shí),主要包括指令、過濾器等,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
詳解如何將angular-ui的圖片輪播組件封裝成一個(gè)指令
本篇文章主要介紹了如何將angular-ui的圖片輪播組件封裝成一個(gè)指令 ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
詳解Monaco?Editor中的Keybinding機(jī)制
這篇文章主要為大家介紹了詳解Monaco?Editor中的Keybinding機(jī)制詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
angular實(shí)現(xiàn)IM聊天圖片發(fā)送實(shí)例
本篇文章主要介紹了angular實(shí)現(xiàn)IM聊天圖片發(fā)送實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
解決Angular.Js與Django標(biāo)簽沖突的方案
AngularJS和django的模板都是用{{}}來引用變量的,這就導(dǎo)致了沖突,所以這篇文章主要就給大家介紹了如何解決Angular.Js與Django標(biāo)簽沖突的方案,有需要的朋友們可以參考借鑒,下面來一起學(xué)習(xí)學(xué)習(xí)吧。2016-12-12

