Commit 94b2222c085e30cbc4a7a49dfac13af15aec98b9

Authored by 啝裳
Committed by GitHub
1 parent 502cc270

perf: improve countTo (#499)

src/components/CountTo/src/index.vue
1 <template> 1 <template>
2 - <span> 2 + <span :style="{ color: color }">
3 {{ displayValue }} 3 {{ displayValue }}
4 </span> 4 </span>
5 </template> 5 </template>
@@ -22,6 +22,7 @@ @@ -22,6 +22,7 @@
22 timestamp: number | null; 22 timestamp: number | null;
23 rAF: any; 23 rAF: any;
24 remaining: number | null; 24 remaining: number | null;
  25 + color: any;
25 }>({ 26 }>({
26 localStartVal: props.startVal, 27 localStartVal: props.startVal,
27 displayValue: formatNumber(props.startVal), 28 displayValue: formatNumber(props.startVal),
@@ -32,6 +33,7 @@ @@ -32,6 +33,7 @@
32 timestamp: null, 33 timestamp: null,
33 remaining: null, 34 remaining: null,
34 rAF: null, 35 rAF: null,
  36 + color: null,
35 }); 37 });
36 38
37 onMounted(() => { 39 onMounted(() => {
@@ -52,10 +54,11 @@ @@ -52,10 +54,11 @@
52 }); 54 });
53 55
54 function start() { 56 function start() {
55 - const { startVal, duration } = props; 57 + const { startVal, duration, color } = props;
56 state.localStartVal = startVal; 58 state.localStartVal = startVal;
57 state.startTime = null; 59 state.startTime = null;
58 state.localDuration = duration; 60 state.localDuration = duration;
  61 + state.color = color;
59 state.paused = false; 62 state.paused = false;
60 state.rAF = requestAnimationFrame(count); 63 state.rAF = requestAnimationFrame(count);
61 } 64 }
src/components/CountTo/src/props.ts
@@ -13,6 +13,10 @@ export const countToProps = { @@ -13,6 +13,10 @@ export const countToProps = {
13 return value >= 0; 13 return value >= 0;
14 }, 14 },
15 }, 15 },
  16 + color: {
  17 + type: String as PropType<string>,
  18 + require: false,
  19 + },
16 decimal: propTypes.string.def('.'), 20 decimal: propTypes.string.def('.'),
17 separator: propTypes.string.def(','), 21 separator: propTypes.string.def(','),
18 prefix: propTypes.string.def(''), 22 prefix: propTypes.string.def(''),
src/views/demo/comp/count-to/index.vue
@@ -2,16 +2,35 @@ @@ -2,16 +2,35 @@
2 <PageWrapper title="数字动画示例"> 2 <PageWrapper title="数字动画示例">
3 <Card> 3 <Card>
4 <CardGrid class="count-to-demo-card"> 4 <CardGrid class="count-to-demo-card">
5 - <CountTo prefix="$" :startVal="1" :endVal="200000" :duration="8000" /> 5 + <CountTo prefix="$" :color="'#409EFF'" :startVal="1" :endVal="200000" :duration="8000" />
6 </CardGrid> 6 </CardGrid>
7 <CardGrid class="count-to-demo-card"> 7 <CardGrid class="count-to-demo-card">
8 - <CountTo suffix="$" :startVal="1" :endVal="300000" :decimals="2" :duration="6000" /> 8 + <CountTo
  9 + suffix="$"
  10 + :color="'red'"
  11 + :startVal="1"
  12 + :endVal="300000"
  13 + :decimals="2"
  14 + :duration="6000"
  15 + />
9 </CardGrid> 16 </CardGrid>
10 <CardGrid class="count-to-demo-card"> 17 <CardGrid class="count-to-demo-card">
11 - <CountTo suffix="$" :startVal="1" :endVal="400000" :duration="7000" /> 18 + <CountTo
  19 + suffix="$"
  20 + :color="'rgb(0,238,0)'"
  21 + :startVal="1"
  22 + :endVal="400000"
  23 + :duration="7000"
  24 + />
12 </CardGrid> 25 </CardGrid>
13 <CardGrid class="count-to-demo-card"> 26 <CardGrid class="count-to-demo-card">
14 - <CountTo separator="-" :startVal="10000" :endVal="500000" :duration="8000" /> 27 + <CountTo
  28 + separator="-"
  29 + :color="'rgba(138,43,226,.6)'"
  30 + :startVal="10000"
  31 + :endVal="500000"
  32 + :duration="8000"
  33 + />
15 </CardGrid> 34 </CardGrid>
16 </Card> 35 </Card>
17 </PageWrapper> 36 </PageWrapper>