這篇文章主要介紹Angular2如何實現(xiàn)搜索和重置按鈕過場動畫,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
在阿城等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站制作、做網(wǎng)站 網(wǎng)站設(shè)計制作按需定制開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站設(shè)計,成都全網(wǎng)營銷,成都外貿(mào)網(wǎng)站建設(shè)公司,阿城網(wǎng)站建設(shè)費用合理。
需求:給項目管理頁面加上搜索和重置的過場動畫。
最先想到的就是利用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">
給重置按鈕和搜索按鈕也來個旋轉(zhuǎn)的特效吧。
最簡單的方案就是利用項目中的bootstrap庫,在搜索或者重置時改變按鈕內(nèi)部的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)
}原理簡單粗暴 即點擊觸發(fā)函數(shù)改變CSS值,2秒后恢復(fù)原有CSS值。。
如果你想再加個彈窗的話可以利用現(xiàn)成的swalert庫;
// 直接在getprojects里面加上如下代碼
swal({
title: 'loading',
type: 'success',
timer: 1000,
showConfirmButton: false,
}).catch(()=>{});
//即每次獲取數(shù)據(jù)后觸發(fā)彈窗動畫。
// 直接在getprojects里面加上如下代碼
swal({
title: 'loading',
type: 'success',
timer: 1000,
showConfirmButton: false,
}).catch(()=>{});
//即每次獲取數(shù)據(jù)后觸發(fā)彈窗動畫。基本效果已經(jīng)實現(xiàn)了,現(xiàn)在把效果復(fù)制到每個組件去
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="請輸入名稱" 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
// 其實也可以直接在模板中調(diào)用getProjects方法,差不多。一個是后期要修改模板,一個是要修改TS文件。
search(pageSize, page, name) {
this.getProjects(pageSize, page, name);
}
//projects.component.ts
// 其實也可以直接在模板中調(diào)用getProjects方法,差不多。一個是后期要修改模板,一個是要修改TS文件。
search(pageSize, page, name) {
this.getProjects(pageSize, page, name);
}qbc-reset實現(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動畫全局復(fù)用機制前,我們先用原生CSS代替。
建立animation.css
.fadeIn{
animation: fadeIn ease-in-out 1.5s 1; // 參數(shù)依次為: 動畫名稱 緩動函數(shù) 動畫時間 動畫運行次數(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ù)依次為: 動畫名稱 緩動函數(shù) 動畫時間 動畫運行次數(shù)
}
@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">
實現(xiàn)效果如下

老鐵還有沒有更簡單的,我不會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如何實現(xiàn)搜索和重置按鈕過場動畫”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
本文名稱:Angular2如何實現(xiàn)搜索和重置按鈕過場動畫
本文來源:http://www.chinadenli.net/article22/gghsjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、企業(yè)網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、網(wǎng)站維護、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)