wang-hao-jie
2022-08-25 2b506494d7c73a3978004bd0b32a5d0783b25efa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<template>
  <div>
    <div
      :class="className"
      :style="{
        color: color,
        fontSize: countSize,
        fontWeight: countWeight,
        display: display,
      }"
    >
      <span>{{ prefix }}</span>
      <span v-cloak :id="id"></span>
      <span>{{ unit }}</span>
      <span>{{ suffix }}</span>
    </div>
    <slot name="intro"></slot>
  </div>
</template>
 
<script>
import { CountUp } from "countup.js";
 
export default {
  data() {
    return {
      unit: "",
      count: {},
    };
  },
  name: "countUp",
  props: {
    id: {
      type: String,
      default: "countUp",
    },
    className: String,
    prefix: String,
    suffix: String,
    display: {
      type: String,
      default: "inline-block",
    },
    endVal: {
      type: Number,
      required: true,
    },
    delay: {
      type: Number,
      default: 0,
    },
    decimalPlaces: {
      type: Number,
      default: 0,
    },
    duration: {
      type: Number,
      default: 2,
    },
    options: {
      type: Object,
      default: () => {
        return {
          startVal: 0,
          useEasing: true,
          useGrouping: true,
          separator: ",",
          decimal: ".",
        };
      },
    },
    color: String,
    countSize: {
      type: String,
      default: "18px",
    },
    countWeight: {
      type: Number,
      default: 500,
    },
  },
  methods: {
    transformValue(val) {
      let endVal = 0;
      let unit = "";
      if (val < 1000000) {
        endVal = val;
      } else if (val >= 1000000 && val < 10000000000) {
        endVal = parseInt(val / 1000000);
        unit = "M+";
      } else if (val >= 10000000000) {
        endVal = parseInt(val / 1000000000);
        unit = "B+";
      }
      return {
        val: endVal,
        unit: unit,
      };
    },
    start() {
      this.$nextTick(() => {
        setTimeout(() => {
          let res = this.transformValue(this.endVal);
          let endVal = res.val;
          this.unit = res.unit;
          let count = {};
          this.options.decimalPlaces = this.decimalPlaces;
          this.options.duration = this.duration;
          this.count = count = new CountUp(this.id, endVal, this.options);
          if (!count.error) {
            count.start();
          }
        }, this.delay);
      });
    },
  },
  mounted() {
    this.start();
  },
  watch: {
    endVal(val) {
      let res = this.transformValue(val);
      let endVal = res.val;
      this.unit = res.unit;
      this.count.update(endVal);
    },
  },
};
</script>