countUp.vue 7.44 KB
<template>
  <div class="chartNum">
    <div class="box-item">
      <!-- ['num_'+item] -->
      <li :class="{'number-item': !isNaN(item), 'mark-item': isNaN(item),'num':index<=currentIndex-1?true:false }" 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 {
  props: {
    count: {
      type: Number,
      default: 0,
    },
  },
  watch: {
    count(newVal) {
      console.log(newVal);
      if (newVal >= 100000) {
        this.currentIndex = 0;
      } else if (newVal >= 10000) {
        this.currentIndex = 1;
      } else if (newVal >= 1000) {
        this.currentIndex = 2;
      } else if (newVal >= 100) {
        this.currentIndex = 3;
      } else if (newVal >= 10) {
        this.currentIndex = 4;
      } else if (newVal >= 0) {
        this.currentIndex = 5;
      }
      this.toOrderNum(newVal);
      this.setNumberTransform();
    },
  },
  data() {
    return {
      currentIndex: 0,
      orderNum: ["0", "0", "0", ",", "0", "0", "0"], // 默认订单总数
    };
  },
  mounted() {
    let newVal = this.count * 1;
    if (newVal >= 100000) {
      this.currentIndex = 0;
    } else if (newVal >= 10000) {
      this.currentIndex = 1;
    } else if (newVal >= 1000) {
      this.currentIndex = 2;
    } else if (newVal >= 100) {
      this.currentIndex = 3;
    } else if (newVal >= 10) {
      this.currentIndex = 4;
    } else if (newVal >= 0) {
      this.currentIndex = 5;
    }
    this.toOrderNum(this.count); // 这里输入数字即可调用
    this.setNumberTransform();
  },
  methods: {
    // 设置文字滚动
    setNumberTransform() {
      const numberItems = this.$refs.numberItem; // 拿到数字的ref,计算元素数量
      const numberArr = this.orderNum.filter((item) => !isNaN(item));
      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 < 6) {
        num = "0" + num; // 如未满八位数,添加"0"补位

        this.toOrderNum(num); // 递归添加"0"补位
      } else if (num.length === 6) {
        // 订单数中加入逗号
        num = num.slice(0, 3) + "," + num.slice(3, 6);
        this.orderNum = num.split(""); // 将其便变成数据,渲染至滚动数组

        //this.setNumberTransform();
      } else {
        // 订单总量数字超过八位显示异常
        this.$message.warning("订单总量数字过大,显示异常,请联系客服");
      }
    },
  },
};
</script>
<style scoped lang='less'>
.num {
  i {
    opacity: 0;
  }
}
// .box-item {
//   .num_0 {
//     &:nth-child(1) {
//       i {
//         opacity: 0;
//       }
//     }
//   }
// }

// .chartNum {
//   &:nth-child(2) {
//     .number-item span {
//       opacity: 0 !important;
//     }
//   }
// }
/*订单总量滚动数字设置*/
.box-item {
  position: relative;
  height: 57px;
  font-size: 40px;
  line-height: 57px;
  text-align: center;
  list-style: none;
  color: #fff;
  writing-mode: vertical-lr;
  text-orientation: upright;
  /*文字禁止编辑*/
}

/* 默认逗号设置 */
.mark-item {
  width: 10px;
  height: 73px;
  margin-right: 5px;
  line-height: 10px;
  font-size: 40px;
  position: relative;

  & > span {
    position: absolute;
    width: 100%;
    bottom: 0;
    writing-mode: vertical-rl;
    text-orientation: upright;
  }
}

/*滚动数字设置*/
.number-item {
  width: 38px;
  height: 57px;
  list-style: none;
  margin-right: 5px;
  background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACcAAAA5CAYAAAC1fzLeAAAAAXNSR0IArs4c6QAAAERlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAJ6ADAAQAAAABAAAAOQAAAADh5OtLAAAIq0lEQVRoBc1Yz4scRRSu7pmd3ckmY0JQCUhyUIjiTbwIsnjQkxrEgxeNf1xQ8aKCuQii4A/QiwdvBjwoigcluJvZzG5mprt933vvq66u7t1JwEBqybzqV+9976tX9bqqU+y+88mrx4vDL8Mj1rYn0zfG4PT45WfDE1eeC4X8NfIHqQ2ikX/+aMr2t2nEthAfkUMtxy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tI+ODEZaimDXBp9RlA4eHDVJT+s7Fitbk4TkrcgXFYgak0TGRKtGy3/tItE3JzgkGjMkifCXjO2Lc0gw7dCQcF0lhWZq3rL1KUGPC5xkhrOw8nAimRO6rfjJyRDDdb44gcaTgc7vlSfzByTi2A52xwbbB6knYCHDGIovoQ1lb1XvFggw7BEY5ZcaoEokBnkBbKxgLhh+S5BFnRv+LLqJUkz59clTZMFw6/aKzMnp0pTkLdBQWf0OAubP3X98XxPt7xs9pI5nKnyAIIkbmN5ooDeaQBjsnUAfgn52IduoOXxYAKI/rJ6fWviItDp0drq8tkQnf7ZsmlQmZH5YQXluPObjbl6vLRarSDSW/AJU9V5nZIaDtE9n5swSCfPPiUzjY2AZi9h9MQCRWFvbN9FWTDb4Nn/z2kmhu3zuWAbgAikh7T9b1xMiV/sFWlerXiV+PEl3vF48pTjEoqWrZLa8cWs4+4bl00sdCk1JRLT8SC1gSyrShSxujEDafFVolcmVYmDS/R4Z4OKlwLev2h3koQPx2JfFNQV3odEkxQZQb/4KrlidjkE/LuP5isyaMkxShgN9Yd0uW1YrcJ/Cpf/V0WAi94AAAAASUVORK5CYII=");
  background-size: 100% 100%;

  & > span {
    position: relative;
    display: inline-block;
    margin-right: 20px;
    width: 100%;
    height: 100%;
    writing-mode: vertical-rl;
    text-orientation: upright;
    overflow: hidden;

    & > i {
      font-style: normal;
      position: absolute;
      top: 8px;
      left: 50%;
      transform: translate(-50%, 0);
      transition: transform 1s ease-in-out;
      letter-spacing: 10px;
      font-family: "fangsong";
      font-weight: 600;
    }
  }
}

.number-item:last-child {
  margin-right: 0;
}
</style>