chenyc
2024-08-19 7ae4f8a626802bc8f027aa091046db97635da879
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<template>
    <div class="layout-pd">
        <el-card shadow="hover" header="数字滚动演示">
            <el-alert
                title="感谢优秀的 `countup.js`,项目地址:https://github.com/inorganik/countUp.js"
                type="success"
                :closable="false"
                class="mb15"
            ></el-alert>
            <el-row :gutter="20">
                <el-col :sm="6" class="mb15" v-for="(v, k) in state.topCardItemList" :key="k">
                    <div class="countup-card-item countup-card-item-box" :style="{ background: `var(${v.color})` }">
                        <div class="countup-card-item-flex" ref="topCardItemRefs">
                            <div class="countup-card-item-title pb3">{{ v.title }}</div>
                            <div class="countup-card-item-title-num pb6"></div>
                            <div class="countup-card-item-tip pb3">{{ v.tip }}</div>
                            <div class="countup-card-item-tip-num"></div>
                        </div>
                        <i :class="v.icon" :style="{ color: v.iconColor }"></i>
                    </div>
                </el-col>
            </el-row>
            <div class="flex-warp">
                <div class="flex-warp-item">
                    <div class="flex-warp-item-box">
                        <el-button type="primary" size="default" @click="refreshCurrent">
                            <el-icon>
                                <ele-RefreshRight />
                            </el-icon>
                            重置/刷新数值
                        </el-button>
                    </div>
                </div>
            </div>
        </el-card>
    </div>
</template>
 
<script setup lang="ts" name="funCountup">
import { reactive, onMounted, nextTick, ref } from 'vue';
import { CountUp } from 'countup.js';
 
// 定义变量内容
const topCardItemRefs = ref<RefType[]>([]);
const state = reactive({
    topCardItemList: [
        {
            title: '今日访问人数',
            titleNum: '123',
            tip: '在场人数',
            tipNum: '911',
            color: '--el-color-primary',
            iconColor: '#ffcb47',
            icon: 'iconfont icon-jinridaiban',
        },
        {
            title: '实验室总数',
            titleNum: '123',
            tip: '使用中',
            tipNum: '611',
            color: '--el-color-success',
            iconColor: '#70cf41',
            icon: 'iconfont icon-AIshiyanshi',
        },
        {
            title: '申请人数(月)',
            titleNum: '123',
            tip: '通过人数',
            tipNum: '911',
            color: '--el-color-warning',
            iconColor: '#dfae64',
            icon: 'iconfont icon-shenqingkaiban',
        },
        {
            title: '销售情况',
            titleNum: '123',
            tip: '销售数',
            tipNum: '911',
            color: '--el-color-danger',
            iconColor: '#e56565',
            icon: 'iconfont icon-ditu',
        },
    ],
});
 
// 初始化数字滚动
const initNumCountUp = () => {
    nextTick(() => {
        topCardItemRefs.value.forEach((v: HTMLDivElement) => {
            new CountUp(v.querySelector('.countup-card-item-title-num') as HTMLDivElement, Math.random() * 10000).start();
            new CountUp(v.querySelector('.countup-card-item-tip-num') as HTMLDivElement, Math.random() * 1000).start();
        });
    });
};
// 重置/刷新数值
const refreshCurrent = () => {
    initNumCountUp();
};
// 页面加载时
onMounted(() => {
    initNumCountUp();
});
</script>
 
<style scoped lang="scss">
.countup-card-item {
    width: 100%;
    height: 103px;
    background: var(--el-text-color-secondary);
    border-radius: 4px;
    transition: all ease 0.3s;
    &:hover {
        box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
        transition: all ease 0.3s;
    }
}
.countup-card-item-box {
    display: flex;
    align-items: center;
    position: relative;
    overflow: hidden;
    &:hover {
        i {
            right: 0px !important;
            bottom: 0px !important;
            transition: all ease 0.3s;
        }
    }
    i {
        position: absolute;
        right: -10px;
        bottom: -10px;
        font-size: 70px;
        transform: rotate(-30deg);
        transition: all ease 0.3s;
    }
    .countup-card-item-flex {
        padding: 0 20px;
        color: var(--el-color-white);
        .countup-card-item-title,
        .countup-card-item-tip {
            font-size: 13px;
        }
        .countup-card-item-title-num {
            font-size: 18px;
        }
        .countup-card-item-tip-num {
            font-size: 13px;
        }
    }
}
</style>