<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      css點滴4—使用css動畫實現領積分效果

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

      1. 整體思路

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

      1.1 半圓圍繞效果

      這個涉及到數學知識,根據角度得到弧度(弧度=角度*圓周率/180),進而換算成坐標,使積分元素圍繞在總積分周圍。關鍵代碼如下:

      this.integral.forEach(i => {
          // 角度轉化為弧度
          let angle = Math.PI / 180 * this.getRandomArbitrary(90, 270)
          // 根據弧度獲取坐標
          i.x = xAxis + 100 * Math.sin(angle)
          i.y = 100 + 100 * Math.cos(angle)
          // 貝塞爾函數
          i.timing = this.timeFun[parseInt(this.getRandomArbitrary(0, 3))]
      })

      注意getRandomArbitrary()函數的功能是獲取隨機數,如下:

      // 求兩個數之間的隨機數
      getRandomArbitrary(min, max) {
          return Math.random() * (max - min) + min;
      }

      timeFunc是一個貝塞爾函數名稱集合,為了實現積分閃爍的效果(上下滑動),定義在data里:

      timeFun: ['ease', 'ease-in', 'ease-in-out', 'ease-out'], // 貝塞爾函數實現閃爍效果

      1.2 積分閃爍(上下滑動)

      用css動畫animation實現積分上下滑動,這里能想到的方式是transform: translateY(5px),就是在y軸上移動一定的距離,并且動畫循環播放。代碼如下:

      .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;
          /*默認貝塞爾函數*/
          animation-timing-function: ease-out;
          /*動畫時間*/
          animation-duration: 1500ms;
          /*動畫循環播放*/
          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);
          }
      }

      注意,我這里除了讓積分上下移動,還讓讓它背景色跟著變化。上下移動的步調不能一致,不然看起來很呆板,所以要使用隨機數函數在貝塞爾函數中隨機選取一個,讓積分小球上下滑動看起來是參差不齊的。關鍵代碼如下:

      /*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'], // 貝塞爾函數實現閃爍效果
      
      // 隨機獲取貝塞爾函數
      i.timing = this.timeFun[parseInt(this.getRandomArbitrary(0, 3))]

       

      1.3 總積分遞增效果

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

      為了使動畫效果看起來平滑,用總時間(1500毫秒)除以小積分個數,得到一個類似動畫關鍵幀的值,這個值作為變化的次數,然后每隔一定時間執行一次。所有動畫時間都設置成1500毫秒,這樣整體效果一致。

      關鍵代碼如下:

      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
              // 修改響應式屬性
              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 小積分消失,總積分膨脹效果

      最后一步就是,小積分沿著總積分的方向運動并消失,總積分膨脹一下。

      小積分運動并消失,x軸坐標移動到總積分的x軸坐標,y軸移動到總積分的y軸坐標,其實就是坐標點變得和總積分一樣,這樣看起來就是沿著中心的方向運動一樣。當所有小積分的坐標運動到這里時候,就可以刪除data數據了。關鍵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;
          }
      }

      總積分膨脹,我這里的實現思路是transform: scale(1.5, 1.5);就是在原來基礎上變大一點,最后再回到原本大小transform: scale(1, 1);,關鍵css如下:

      .totalAdd {
          animation-name: totalScale;
          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 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);
          }
      }

      至此,整個動畫的邏輯就理清了,先寫個demo,代碼我已經放在github上了,積分動畫

      效果如下:

      2. 在項目中落地

      最后在項目中,涉及到一個ajax請求,就是領取積分,只需要把動畫放在這個ajax請求成功回調里就大功告成了。js關鍵代碼如下:

      // 一鍵領取積分
      aKeyReceive() {
          if (this.unreceivedIntegral.length === 0) {
              return bottomTip("暫無未領積分")
          }
          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()
          }
      },
      // 領取積分的動畫
      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)
          })
      }

      最后項目上線后的效果如下:

      注意,這里頁面閃一下是的原因是ajax請求里有一個loading狀態,其實如果服務端完全可靠的話,可有可無。

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

       

      posted @ 2020-04-13 15:08  nd  閱讀(3141)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 成人自拍小视频免费观看| 久久99热只有频精品8| 亚洲岛国av一区二区| 国产精品成人久久电影| 蜜臀av人妻国产精品建身房| 亚洲国产色婷婷久久99精品91| 开心久久综合激情五月天| 给我播放片在线观看| 午夜欧美精品久久久久久久| 肉大捧一进一出免费视频| 亚洲av日韩av永久无码电影| 亚洲第一精品一二三区| 国产无套内射又大又猛又粗又爽| 久久久久噜噜噜亚洲熟女综合| 色婷婷亚洲精品综合影院| 亚洲精品一区二区动漫| 国内熟妇人妻色在线三级| 福利无遮挡喷水高潮| 成人免费在线播放av| 国产精品一区二区国产馆| 免费网站看sm调教视频| 日韩午夜福利视频在线观看| 国产不卡一区二区在线| 无码人妻精品一区二区三区下载| 亚洲人成网站在线播放动漫| 亚洲不卡av不卡一区二区| 中文字幕无码不卡在线| 一区二区福利在线视频| 人人妻人人爽人人澡av| 九九热在线视频免费播放| 免费无码AV一区二区波多野结衣 | 欧美黑人巨大xxxxx| 国产福利免费在线观看| 午夜视频免费试看| 亚洲熟妇一区二区三个区| 亚洲人成网站999久久久综合 | 久久久久久久波多野结衣高潮| 国产av国片精品一区二区| 国产精品 无码专区| 亚洲一区二区三区在线| 成人国产av精品免费网|