<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

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

          共 6292字,需瀏覽 13分鐘

           ·

          2021-12-20 16:06

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

          效果可以看文末動(dòng)圖。

          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),還讓讓它背景色跟著變化。

          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)畫。

          效果如下:

          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)目上線后的效果如下:

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

          最后各位看官如果有類似需求,不妨借鑒一下,有更好的思路也提出來我參考一下。


          作者:Tyler Ning
          原文:http://www.cnblogs.com/tylerdonet/

          猜你喜歡
          瀏覽 55
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  五月欧美激激激综合网色播 | 亚洲人成人无码网www国产 | 男女办事网 | 亚洲国产精品自拍 | 一级黄色淫秽视频免费观看 |