Vue中使用vue-count-to(数字滚动插件)

发布时间 2023-10-24 16:03:34作者: Mahmud(مەھمۇد)

效果图

1. 简单介绍 

npm官网:vue-count-to

vue-count-to 就是一个组件插件,咱们引入进来,可以去打印一下,它就是一个组件实例,使用components注册一下,就可以在模板中使用了,具体方式如下: 

 

2. 安装 

npm install vue-count-to

3. 引入

import CountTo from 'vue-count-to'

4. 注册 

components: {
  CountTo

5. 模板中使用

<CountTo :startVal='startVal' :endVal='endVal' :duration='duration'>
    //startVal:开始节点 endVal:接受节点  duration:时间

 

6. 完整的代码 

<template>
  <div>
    <el-row :gutter="20">
      <el-col :xs="24" :sm="12" :lg="6" v-for="item of lists" :key="item.name">
        <div class="grid-content bg-purple" :style="{background:item.color}">
          <!--动态绑定style-->
          <div class="card-panel-description">
            <div class="card-panel-text">{{ item.name }}</div>
            <CountTo :startVal='0' :endVal='item.number' :duration='item.duration'/>
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import CountTo from 'vue-count-to'
export default {
  components: {
    CountTo,
  },
  data() {
    return {
      lists: [
        {
          name: "最高可借金额",
          color: "#67ca3a",
          number: 13594,
          duration: 1500,
        },
        {
          name: "回报率",
          color: "#f60",
          number: 9833,
          duration: 1500,
        },
        {
          name: "业绩领跑",
          color: "#f56c6c",
          number: 8888,
          duration: 1500,
        },
        {
          name: "安稳底薪战队",
          color: "#409eff",
          number: 6666,
          duration: 1500,
        },
      ],
    }

  },
  created() {
  },
  methods: {}
}

</script>
<style scoped lang="less">
.card-panel-description {
  height: 150px;
  line-height: 40px;
  font-weight: bolder;
  font-size: larger;
  color: white;
  text-align: center;
}

.card-panel-text {
  font-weight: 800;
  font-size: larger;
  height: 60px;
  line-height: 60px;
  color: black;
}

.el-row {
  margin-bottom: 20px;

  &:last-child {
    margin-bottom: 0;
  }
}

.el-col {
  border-radius: 4px;
}

.bg-purple-dark {
  background: #99a9bf;
}

.bg-purple {
  background: #d3dce6;
}

.bg-purple-light {
  background: #e5e9f2;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
</style>