溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

css如何實(shí)現(xiàn)領(lǐng)積分動(dòng)畫效果

發(fā)布時(shí)間:2021-01-21 09:27:04 來源:億速云 閱讀:303 作者:小新 欄目:web開發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)css如何實(shí)現(xiàn)領(lǐng)積分動(dòng)畫效果,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

最近項(xiàng)目中要做一個(gè)領(lǐng)積分的效果,根據(jù)老板的描述,這個(gè)效果類似于支付寶螞蟻森林里的領(lǐng)取能量。整體效果是就是在樹周圍飄著幾個(gè)積分元素,上下滑動(dòng),類似星星閃爍,點(diǎn)擊領(lǐng)取后,沿著樹中心的位置滑動(dòng)并消失,樹上的能量遞增,最后膨脹,變大一點(diǎn)。

1. 整體思路

首先想到基本輪廓是一個(gè)地球,周圍半圓范圍內(nèi)圍繞著好幾個(gè)閃爍的小星星,然后同時(shí)墜落到地球上。用到css定位,border-radius畫圓,animation動(dòng)畫,點(diǎn)擊動(dòng)作觸發(fā)新的動(dòng)畫,積分遞增效果類似于countUp.js,但是這里不用這個(gè)插件,手動(dòng)實(shí)現(xiàn)。

1.1 半圓圍繞效果

這個(gè)涉及到數(shù)學(xué)知識(shí),根據(jù)角度得到弧度(弧度=角度*圓周率/180),進(jìn)而換算成坐標(biāo),使積分元素圍繞在總積分周圍。關(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ù)名稱集合,為了實(shí)現(xiàn)積分閃爍的效果(上下滑動(dòng)),定義在data里:

timeFun: ['ease', 'ease-in', 'ease-in-out', 'ease-out'], // 貝塞爾函數(shù)實(shí)現(xiàn)閃爍效果

1.2 積分閃爍(上下滑動(dòng))

用css動(dòng)畫animation實(shí)現(xiàn)積分上下滑動(dòng),這里能想到的方式是transform: translateY(5px),就是在y軸上移動(dòng)一定的距離,并且動(dòng)畫循環(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)畫時(shí)間*/
    animation-duration: 1500ms;
    /*動(dòng)畫循環(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),還讓讓它背景色跟著變化。上下移動(dòng)的步調(diào)不能一致,不然看起來很呆板,所以要使用隨機(jī)數(shù)函數(shù)在貝塞爾函數(shù)中隨機(jī)選取一個(gè),讓積分小球上下滑動(dòng)看起來是參差不齊的。關(guān)鍵代碼如下:

/*html*/
<div :class="integralClass"
     v-for="item in integral"
     :data-angle="item.angle"
     :style="{ left: item.x + 'px', top: item.y + 'px', animationTimingFunction: item.timing}">{{item.value}}
</div>
/*js*/
// data中定義
timeFun: ['ease', 'ease-in', 'ease-in-out', 'ease-out'], // 貝塞爾函數(shù)實(shí)現(xiàn)閃爍效果

// 隨機(jī)獲取貝塞爾函數(shù)
i.timing = this.timeFun[parseInt(this.getRandomArbitrary(0, 3))]

1.3 總積分遞增效果

點(diǎn)擊領(lǐng)取之后積分,總積分要累加起來,這個(gè)類似countUp.js的效果,但是這里不能為了這一個(gè)功能引用這個(gè)插件。項(xiàng)目是使用vue.js,很容易就想到修改data的響應(yīng)式屬性讓數(shù)字變化,關(guān)鍵是如何讓這個(gè)變化不是一下就變過來,而是漸進(jìn)的。我這里思路是Promise+setTimeout,每隔一定時(shí)間修改一次data屬性,這樣看起來就不是突然變化的。

為了使動(dòng)畫效果看起來平滑,用總時(shí)間(1500毫秒)除以小積分個(gè)數(shù),得到一個(gè)類似動(dòng)畫關(guān)鍵幀的值,這個(gè)值作為變化的次數(shù),然后每隔一定時(shí)間執(zhí)行一次。所有動(dòng)畫時(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)變得和總積分一樣,這樣看起來就是沿著中心的方向運(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);就是在原來基礎(chǔ)上變大一點(diǎn),最后再回到原本大小transform: scale(1, 1);,關(guān)鍵css如下:

.totalAdd {
    animation-name: totalScale;
    animation-timing-function: ease-in-out;
    /*動(dòng)畫只播放一次*/
    animation-iteration-count: 1;
    /*動(dòng)畫停留在最后一個(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)畫的邏輯就理清了,先寫個(gè)demo,代碼我已經(jīng)放在github上了,積分動(dòng)畫。

效果如下:

css如何實(shí)現(xiàn)領(lǐng)積分動(dòng)畫效果

2. 在項(xiàng)目中落地

最后在項(xiàng)目中,涉及到一個(gè)ajax請(qǐng)求,就是領(lǐng)取積分,只需要把動(dòng)畫放在這個(gè)ajax請(qǐng)求成功回調(diào)里就大功告成了。js關(guān)鍵代碼如下:

// 一鍵領(lǐng)取積分
aKeyReceive() {
    if (this.unreceivedIntegral.length === 0) {
        return bottomTip("暫無未領(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)畫
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)目上線后的效果如下:

css如何實(shí)現(xiàn)領(lǐng)積分動(dòng)畫效果

注意,這里頁面閃一下是的原因是ajax請(qǐng)求里有一個(gè)loading狀態(tài),其實(shí)如果服務(wù)端完全可靠的話,可有可無。

關(guān)于“css如何實(shí)現(xiàn)領(lǐng)積分動(dòng)畫效果”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

css
AI