1. 程式人生 > >Vue.js大屏數字滾動翻轉效果

Vue.js大屏數字滾動翻轉效果

 大屏數字滾動翻轉效果來源於最近工作中element後臺管理頁面一張大屏的UI圖,該UI圖上有一個模組需要有數字往上翻動的效果,以下是最終實現的效果:

整體思路:

在實現此效果之前,我們先來捋一下思路,用思維導圖來設計一下我們的實現步驟,如下:

 

 

你可以審查元素,下載數字背景圖片,複製圖片地址,或者使用其他背景圖片、背景顏色

 

有了以上的設計流程,我們先來簡單實現一下:

// CSS程式碼
<style>
.box-item {
  position: relative;
  display: inline-block;
  width: 54px;
  height: 82px;
  /* 背景圖片 */
  background: url(./number-bg.png) no-repeat center center;
  background-size: 100% 100%;
  font-size: 62px;
  line-height: 82px;
  text-align: center;
}
</style>

// htm程式碼
<div class="box">
  <p class="box-item">
    <span>1</span>
   </p>
</div>

 

實現以上程式碼後,它的效果將是下面這樣的:

 

 

 

思考:背景框中有了數字以後,我們現在來思考一下,背景框中的文字,一定是 0-9 之前的數字,要在不打亂以上 html 結構的前提下,如何讓數字滾動起來呢?這個時候我們的魔爪就伸向了一個 CSS 屬性: writing-mode ,下面是它屬性的介紹:

  • horizontal-tb:預設值,表示水平排版,從上到下。
  • vertical-lr:表示垂直排版,從左到右。
  • vertical-rl:表示垂直排版,從右到左。

 

它的實時效果是像下面這樣:

 

 

根據以上的靈感,我們可以實現下面這樣的效果:

 

程式碼如下:

// html部分
<p class="box-item">
  <span>0123456789</span>
</p>

// style部分
.box-item {
  display: inline-block;
  width: 54px;
  height: 82px;
  background: url(./number-bg.png) no-repeat center center;
  background-size: 100% 100%;
  font-size: 62px;
  line-height: 82px;
  text-align: center;
  position: relative;
  writing-mode: vertical-lr;
  text-orientation: upright;
  /* overflow: hidden; */
}
.box-item span {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  letter-spacing: 10px;
}

 

計算滾動

如果我們想讓數字滾動到 5 ,那麼滾動的具體到底是多少?

答案是:向下滾動 -50%

那麼其他的數字呢?

得益於我們特殊的實現方法,每一位數字的滾動距離有一個通用的公式:

transform: `translate(-50%,-${number * 10}%)

有了以上公式,我們讓數字滾動到 5 ,它的效果如下:

 

 

程式碼加上  `transform: `translate(-50%,-${number * 10}%)`,示例如下

.box-item span {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translate(-50%,-50%);
  letter-spacing: 10px;
}

 

滾動動畫的實現

在知道了每一個數字具體的滾動距離後,我們來設計一下,讓數字能夠隨機滾動起來:

 

 

一下是讓數字隨機滾動的JS

setInterval(() => {
  let number = document.getElementById('Number')
  let random = getRandomNumber(0,10)
  number.style.transform = `translate(-50%, -${random * 10}%)`
}, 2000)
function getRandomNumber (min, max) {
  return Math.floor(Math.random() * (max - min + 1) + min)
}

 

至此,我們數字滾動效果已經初步實現了,在下一節中我們將會逐步完善此效果,以滿足業務需求。

 

完善

在上一節中,我們初步完成了滾動的效果,這一節我們將根據最開始的思維導圖來設計一個通用的 Vue 業務元件

 

因為我們的業務需要,我們最大的位數是 8 位數字,所以對不足八位進行補位:

假如傳遞的位數不足 8 位,我們需要對它進行補 0 的操作,補 0 完成以後,我們也需要把它轉換成金額的格式

toOrderNum(num) {
      num = num.toString()
      // 把訂單數變成字串
      if (num.length < 8) {
        num = '0' + num // 如未滿八位數,新增"0"補位
        this.toOrderNum(num) // 遞迴新增"0"補位
      } else if (num.length === 8) {
        // 訂單數中加入逗號
        num = num.slice(0, 2) + ',' + num.slice(2, 5) + ',' + num.slice(5, 8)
        this.orderNum = num.split('') // 將其便變成資料,渲染至滾動陣列
      } else {
        // 訂單總量數字超過八位顯示異常
        this.$message.warning('訂單總量數字過大,顯示異常,請聯絡客服')
      }
    },

 

渲染

我們根據上面補位字串,分隔成字元陣列,在頁面中進行渲染:

computeNumber:為字元陣列,例如:['0','0',',','0','0','0',',','9','1','7']

// html程式碼
<ul>
  <li
    :class="{'number-item': !isNaN(item) }"
    v-for="(item,index) in computeNumber"
    :key="index"
  >
    <span v-if="!isNaN(item)">
      <i ref="numberItem">0123456789</i>
    </span>
    <span v-else>{{item}}</span>
  </li>
</ul>


// CSS程式碼
.number-item {
  width: 50px;
  background: url(./number-bg.png) no-repeat center center;
  background-size:100% 100%;
  & > span {
    position: relative;
    display: inline-block;
    margin-right: 10px;
    width: 100%;
    height: 100%;
    writing-mode: vertical-rl;
    text-orientation: upright;
    overflow: hidden;
    & > i {
      position: absolute;
      top: 0;
      left: 50%;
      transform: translate(-50%,0);
      transition: transform 0.5s ease-in-out;
      letter-spacing: 10px;
    }
  }
}

 

頁面渲染效果:

 

 

數字隨機增長,模擬輪詢效果

頁面渲染完畢後,我們來讓數字滾動起來,設計如下兩個方法,其中 increaseNumber 需要在 Vue 生命週期 mounted 函式中呼叫

// 定時增長數字
increaseNumber () {
  let self = this
  this.timer = setInterval(() => {
    self.newNumber = self.newNumber + getRandomNumber(1, 100)
    self.setNumberTransform()
  }, 3000)
},
// 設定每一位數字的偏移
setNumberTransform () {
  let numberItems = this.$refs.numberItem
  let numberArr = this.computeNumber.filter(item => !isNaN(item))
  for (let index = 0; index < numberItems.length; index++) {
    let elem = numberItems[index]
    elem.style.transform = `translate(-50%, -${numberArr[index] * 10}%)`
  }
}

 

最終實現效果:

 

 

完整程式碼如下:

<template>
    <div class="chartNum">
                <h3 class="orderTitle">訂單總量</h3>
                <div class="box-item">
                    <li :class="{'number-item': !isNaN(item), 'mark-item': isNaN(item) }"
                        v-for="(item,index) in orderNum"
                        :key="index">
                            <span v-if="!isNaN(item)">
                              <i ref="numberItem">0123456789</i>
                            </span>
                        <span class="comma" v-else>{{item}}</span>
                    </li>
                </div>
            </div>
</template>
<script>
    export default {
        data() {
            return {
                orderNum: ['0', '0', ',', '0', '0', '0', ',', '0', '0', '0'], // 預設訂單總數
            }
        }
        mounted: {
            this.toOrderNum(num) // 這裡輸入數字即可呼叫
        },
        methods: {
               // 設定文字滾動
            setNumberTransform () {
              const numberItems = this.$refs.numberItem // 拿到數字的ref,計算元素數量
              const numberArr = this.orderNum.filter(item => !isNaN(item))
              // 結合CSS 對數字字元進行滾動,顯示訂單數量
              for (let index = 0; index < numberItems.length; index++) {
                const elem = numberItems[index]
                elem.style.transform = `translate(-50%, -${numberArr[index] * 10}%)`
              }
            },
            // 處理總訂單數字
            toOrderNum(num) {
              num = num.toString()
              // 把訂單數變成字串
              if (num.length < 8) {
                num = '0' + num // 如未滿八位數,新增"0"補位
                this.toOrderNum(num) // 遞迴新增"0"補位
              } else if (num.length === 8) {
                // 訂單數中加入逗號
                num = num.slice(0, 2) + ',' + num.slice(2, 5) + ',' + num.slice(5, 8)
                this.orderNum = num.split('') // 將其便變成資料,渲染至滾動陣列
              } else {
                // 訂單總量數字超過八位顯示異常
                this.$message.warning('訂單總量數字過大,顯示異常,請聯絡客服')
              }
            },
        }
    }
</script>
<style scoped lang='scss'>
     /*訂單總量滾動數字設定*/
    .box-item {
        position: relative;
        height: 100px;
        font-size: 54px;
        line-height: 41px;
        text-align: center;
        list-style: none;
        color: #2D7CFF;
        writing-mode: vertical-lr;
        text-orientation: upright;
        /*文字禁止編輯*/
        -moz-user-select: none; /*火狐*/
        -webkit-user-select: none; /*webkit瀏覽器*/
        -ms-user-select: none; /*IE10*/
        -khtml-user-select: none; /*早期瀏覽器*/
        user-select: none;
        /* overflow: hidden; */
    }
    /* 預設逗號設定 */
    .mark-item {
        width: 10px;
        height: 100px;
        margin-right: 5px;
        line-height: 10px;
        font-size: 48px;
        position: relative;
        & > span {
            position: absolute;
            width: 100%;
            bottom: 0;
            writing-mode: vertical-rl;
            text-orientation: upright;
        }
    }
    /*滾動數字設定*/
    .number-item {
        width: 41px;
        height: 75px;
        background: #ccc;
        list-style: none;
        margin-right: 5px;
        background:rgba(250,250,250,1);
        border-radius:4px;
        border:1px solid rgba(221,221,221,1);
        & > span {
            position: relative;
            display: inline-block;
            margin-right: 10px;
            width: 100%;
            height: 100%;
            writing-mode: vertical-rl;
            text-orientation: upright;
            overflow: hidden;
            & > i {
                font-style: normal;
                position: absolute;
                top: 11px;
                left: 50%;
                transform: translate(-50%,0);
                transition: transform 1s ease-in-out;
                letter-spacing: 10px;
            }
        }
    }
    .number-item:last-child {
        margin-right: 0;
    }
</style>

 

 

如果有任何疑問即可留言反饋,會在第一時間回覆反饋,謝謝大家