這篇文章給大家分享的是有關(guān)CSS動(dòng)畫(huà)怎么實(shí)現(xiàn)領(lǐng)積分效果的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
成都創(chuàng)新互聯(lián)公司從2013年開(kāi)始,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元中站做網(wǎng)站,已為上家服務(wù),為中站各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108
1. 整體思路
首先想到基本輪廓是一個(gè)地球,周?chē)雸A范圍內(nèi)圍繞著好幾個(gè)閃爍的小星星,然后同時(shí)墜落到地球上。用到css定位,border-radius畫(huà)圓,animation動(dòng)畫(huà),點(diǎn)擊動(dòng)作觸發(fā)新的動(dòng)畫(huà),積分遞增效果類(lèi)似于 countUp.js ,但是這里不用這個(gè)插件,手動(dòng)實(shí)現(xiàn)。
1.1 半圓圍繞效果
這個(gè)涉及到數(shù)學(xué)知識(shí),根據(jù)角度得到弧度(弧度=角度*圓周率/180),進(jìn)而換算成坐標(biāo),使積分元素圍繞在總積分周?chē)jP(guān)鍵代碼如下:
this.integral.forEach(i => {
// 角度轉(zhuǎn)化為弧度
let angle = Math.PI / 180 * this.getRandomArbitrary(90, 270)
// 根據(jù)弧度獲取坐標(biāo)
i.x = xAxis + 100 * Math.sin(angle)
i.y = 100 + 100 * Math.cos(angle)
// 貝塞爾函數(shù)
i.timing = this.timeFun[parseInt(this.getRandomArbitrary(0, 3))]
})注意getRandomArbitrary()函數(shù)的功能是獲取隨機(jī)數(shù),如下:
// 求兩個(gè)數(shù)之間的隨機(jī)數(shù)
getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}timeFunc是一個(gè)貝塞爾函數(shù)名稱(chēng)集合,為了實(shí)現(xiàn)積分閃爍的效果(上下滑動(dòng))
1.2 積分閃爍(上下滑動(dòng))
用css動(dòng)畫(huà)animation實(shí)現(xiàn)積分上下滑動(dòng),這里能想到的方式是transform: translateY(5px),就是在y軸上移動(dòng)一定的距離,并且動(dòng)畫(huà)循環(huán)播放。代碼如下:
.foo {
display: flex;
font-size: 10px;
align-items: center;
justify-content: center;
width: 30px;
height: 30px;
position: fixed;
top: 0;
left: 0;
animation-name: slideDown;
/*默認(rèn)貝塞爾函數(shù)*/
animation-timing-function: ease-out;
/*動(dòng)畫(huà)時(shí)間*/
animation-duration: 1500ms;
/*動(dòng)畫(huà)循環(huán)播放*/
animation-iteration-count: infinite;
-moz-box-shadow: -5px -5px 10px 3px rgb(277, 102, 63) inset;
-webkit-box-shadow: -5px -5px 10px 3px rgb(277, 102, 63) inset;
box-shadow: -5px -5px 10px 3px rgb(277, 102, 63) inset;
}
/*小積分上下閃爍*/
@keyframes slideDown {
from {
transform: translateY(0);
}
50% {
transform: translateY(5px);
background-color: rgb(255, 234, 170);
}
to {
transform: translateY(0);
background: rgb(255, 202, 168);
}
}注意,我這里除了讓積分上下移動(dòng),還讓讓它背景色跟著變化。
1.3 總積分遞增效果
點(diǎn)擊領(lǐng)取之后積分,總積分要累加起來(lái),這個(gè)類(lèi)似countUp.js的效果,但是這里不能為了這一個(gè)功能引用這個(gè)插件。項(xiàng)目是使用vue.js,很容易就想到修改data的響應(yīng)式屬性讓數(shù)字變化,關(guān)鍵是如何讓這個(gè)變化不是一下就變過(guò)來(lái),而是漸進(jìn)的。我這里思路是Promise+setTimeout,每隔一定時(shí)間修改一次data屬性,這樣看起來(lái)就不是突然變化的。
為了使動(dòng)畫(huà)效果看起來(lái)平滑,用總時(shí)間(1500毫秒)除以小積分個(gè)數(shù),得到一個(gè)類(lèi)似動(dòng)畫(huà)關(guān)鍵幀的值,這個(gè)值作為變化的次數(shù),然后每隔一定時(shí)間執(zhí)行一次。所有動(dòng)畫(huà)時(shí)間都設(shè)置成1500毫秒,這樣整體效果一致。
關(guān)鍵代碼如下:
this.integralClass.fooClear = true
this.totalClass.totalAdd = true
this.totalText = `${this.totalIntegral}積分`
let count = this.integral.length, timeoutID = null, tasks = [], totalTime = parseInt(1500 / count)
const output = (i) => new Promise((resolve) => {
timeoutID = setTimeout(() => {
// 積分遞增
this.totalIntegral += this.integral[i].value
// 修改響應(yīng)式屬性
this.totalText = `${this.totalIntegral}積分`
resolve();
}, totalTime * i);
})
for (var i = 0; i < 5; i++) {
tasks.push(output(i));
}
Promise.all(tasks).then(() => {
clearTimeout(timeoutID)
})1.4 小積分消失,總積分膨脹效果
最后一步就是,小積分沿著總積分的方向運(yùn)動(dòng)并消失,總積分膨脹一下。
小積分運(yùn)動(dòng)并消失,x軸坐標(biāo)移動(dòng)到總積分的x軸坐標(biāo),y軸移動(dòng)到總積分的y軸坐標(biāo),其實(shí)就是坐標(biāo)點(diǎn)變得和總積分一樣,這樣看起來(lái)就是沿著中心的方向運(yùn)動(dòng)一樣。當(dāng)所有小積分的坐標(biāo)運(yùn)動(dòng)到這里時(shí)候,就可以刪除data數(shù)據(jù)了。關(guān)鍵css如下:
.fooClear {
animation-name: clearAway;
animation-timing-function: ease-in-out;
animation-iteration-count: 1;
animation-fill-mode: forwards;
-webkit-animation-duration: 1500ms;
-moz-animation-duration: 1500ms;
-o-animation-duration: 1500ms;
animation-duration: 1500ms;
}
/*清除小的積分*/
@keyframes clearAway {
to {
top: 150px;
left: 207px;
opacity: 0;
visibility: hidden;
width: 0;
height: 0;
}
}總積分膨脹,我這里的實(shí)現(xiàn)思路是transform: scale(1.5, 1.5);就是在原來(lái)基礎(chǔ)上變大一點(diǎn),最后再回到原本大小transform: scale(1, 1);,關(guān)鍵css如下:
.totalAdd {
animation-name: totalScale;
animation-timing-function: ease-in-out;
/*動(dòng)畫(huà)只播放一次*/
animation-iteration-count: 1;
/*動(dòng)畫(huà)停留在最后一個(gè)關(guān)鍵幀*/
animation-fill-mode: forwards;
-webkit-animation-duration: 1500ms;
-moz-animation-duration: 1500ms;
-o-animation-duration: 1500ms;
animation-duration: 1500ms;
}
@keyframes totalScale {
50% {
transform: scale(1.15, 1.15);
-ms-transform: scale(1.15, 1.15);
-moz-transform: scale(1.15, 1.15);
-webkit-transform: scale(1.15, 1.15);
-o-transform: scale(1.15, 1.15);
}
to {
transform: scale(1, 1);
-ms-transform: scale(1, 1);
-moz-transform: scale(1, 1);
-webkit-transform: scale(1, 1);
-o-transform: scale(1, 1);
}
}至此,整個(gè)動(dòng)畫(huà)的邏輯就理清了,先寫(xiě)個(gè)demo,代碼我已經(jīng)放在github上了, 積分動(dòng)畫(huà) 。
效果如下:

2. 在項(xiàng)目中落地
最后在項(xiàng)目中,涉及到一個(gè)ajax請(qǐng)求,就是領(lǐng)取積分,只需要把動(dòng)畫(huà)放在這個(gè)ajax請(qǐng)求成功回調(diào)里就大功告成了。js關(guān)鍵代碼如下:
// 一鍵領(lǐng)取積分
aKeyReceive() {
if (this.unreceivedIntegral.length === 0) {
return bottomTip("暫無(wú)未領(lǐng)積分")
}
if (this.userInfo.memberAKeyGet) {
let param = {
memberId: this.userInfo.memberId,
integralIds: this.unreceivedIntegral.map(u => u.id).join(","),
integralValue: this.unreceivedIntegral.reduce((acc, curr, index, arr) => { return acc + curr.value }, 0)
}
this.$refs.resLoading.show(true)
api.getAllChangeStatus(param).then(res => {
let data = res.data
if (data.success) {
this.getRecordIntegralList()
this.playIntegralAnim()
} else {
bottomTip(data.message)
}
}).finally(() => {
this.$refs.resLoading.show(false)
})
} else {
this.$refs.refPopTip.show()
}
},
// 領(lǐng)取積分的動(dòng)畫(huà)
playIntegralAnim() {
this.integralClass.fooClear = true
this.totalClass.totalAdd = true
this.totalText = `${this.statisticsData.useValue}積分`
let count = this.unreceivedIntegral.length, timeoutID = null, tasks = [], totalTime = parseInt(1500 / count)
const output = (i) => new Promise((resolve) => {
timeoutID = setTimeout(() => {
this.statisticsData.useValue += this.unreceivedIntegral[i].value
this.totalText = `${this.statisticsData.useValue}積分`
resolve();
}, totalTime * i);
})
for (let i = 0; i < count; i++) {
tasks.push(output(i));
}
Promise.all(tasks).then(() => {
clearTimeout(timeoutID)
})
}最后項(xiàng)目上線后的效果如下:

注意,這里頁(yè)面閃一下是的原因是ajax請(qǐng)求里有一個(gè)loading狀態(tài),其實(shí)如果服務(wù)端完全可靠的話,可有可無(wú)。
感謝各位的閱讀!關(guān)于“CSS動(dòng)畫(huà)怎么實(shí)現(xiàn)領(lǐng)積分效果”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
文章題目:CSS動(dòng)畫(huà)怎么實(shí)現(xiàn)領(lǐng)積分效果
網(wǎng)頁(yè)URL:http://chinadenli.net/article32/gdggpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、自適應(yīng)網(wǎng)站、網(wǎng)站設(shè)計(jì)、網(wǎng)站收錄、網(wǎng)站營(yíng)銷(xiāo)、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
網(wǎng)頁(yè)設(shè)計(jì)公司知識(shí)