TransitionNumber.vue 1.58 KB
<template>
  <div class="number-box flex">
    <template v-if="numList.length">
      <template v-for="(i,index) in numList">
        <div :key="index">
          {{i}}
        </div>
        <template v-if="index != numList.length - 1">,</template>
      </template>
    </template>
    <template v-else>
      <div
        class="transition-number"
        :id="idKey"
      >
      </div>
    </template>
  </div>

</template>

<script type='text/ecmascript-6'>
export default {
  name: 'TransitionNumber',
  props: {
    num: [String, Number],
    idKey: String,
    split: Boolean
  },
  data () {
    return {
      numList: []
    }
  },
  watch: {
    num (n) {
      this.numList = []
      setTimeout(() => {
        if (n > 0) {
          document.querySelector(`#${this.idKey}`).style = `--step:${n}`
          setTimeout(() => {
            this.numList = this.setPrice(n)
          }, 1000)
        } else document.querySelector(`#${this.idKey}`).style = `--step:0`
      }, 200)
    }
  },
  mounted () {
  },
  methods: {
    setPrice (price) {
      let number = (price.toString() || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
      return number.split(',')
    }
  }
}
</script>

<style scoped lang='scss'>
.number-box {
}
.transition-number {
  color: inherit;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  --step: 0;
  counter-reset: count var(--step);
  z-index: var(--step);
  transition: --step 1s;
  font-size: inherit;
  &::before {
    content: counter(count);
  }
}
@property --step {
  syntax: '<integer>';
  inherits: false;
  initial-value: 15;
}
</style>