Vue中使用vue-count-to(数字滚动插件)详细教程
作者:会说法语的猪
这篇文章主要给大家介绍了关于Vue中使用vue-count-to(数字滚动插件)的相关资料,最近需要开发一个数字滚动效果,在网上找到一个关于vue-countTo的插件,觉得这个插件还不错,需要的朋友可以参考下
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' />
6. 测试完整代码
<template>
  <div class="vue-count-to">
    <div class="count-to">
      <div>
        <CountTo :startVal='startVal' :endVal='endVal' :duration='duration' />
      </div>
      <div>
        <CountTo :startVal='startVal' :endVal='endVal' :duration='duration' />
      </div>
      <div>
        <CountTo :startVal='startVal' :endVal='endVal' :duration='duration' />
      </div>
      <div>
        <CountTo :startVal='startVal' :endVal='endVal' :duration='duration' />
      </div>
      <div>
        <CountTo :startVal='startVal' :endVal='endVal' :duration='duration' />
      </div>
      <div>
        <CountTo :startVal='startVal' :endVal='endVal' :duration='duration' />
      </div>
      <div>
        <CountTo :startVal='startVal' :endVal='endVal' :duration='duration' />
      </div>
      <div>
        <CountTo :startVal='startVal' :endVal='endVal' :duration='duration' />
      </div>
    </div>
  </div>
</template>
<script>
import CountTo from 'vue-count-to'
export default {
  data() {
    return {
      startVal: 0,
      endVal: 100,
      duration: 3000,
      timer: null
    }
  },
  components: {
    CountTo
  },
  mounted(){
    this.timer = setInterval(() => {
      this.endVal = this.endVal * 2
    }, 4000)
  },
  destroyed() {
    clearInterval(this.timer)
  },
}
</script>
<style scoped>
.vue-count-to {
  width: 100%;
  height: 100%;
}
.count-to {
  width: 300px;
  height: 300px;
  margin: 100px 0 0 100px;
  border: 1px solid red;
}
.count-to span {
  font-size: 30px;
  font-weight: 700;
  font-family: 'YJSZ';
}
.count-to > div:nth-of-type(1) > span {
  color: red;
}
.count-to > div:nth-of-type(2) > span {
  color: blue;
}
.count-to > div:nth-of-type(3) > span {
  color: pink;
}
.count-to > div:nth-of-type(4) > span {
  color: yellow;
}
.count-to > div:nth-of-type(5) > span {
  color: green;
}
.count-to > div:nth-of-type(6) > span {
  color: orange;
}
.count-to > div:nth-of-type(7) > span {
  color: cyan;
}
.count-to > div:nth-of-type(8) > span {
  color: purple;
}
</style>7. 效果

实际滚动的是很流畅的哈;可能是我这个工具的问题
需要了解的是:
vue-count-to实际编译出来的就是个span标签所以我们在给其写样式的时候可以直接用span标签;应该也可以直接在上面写class类名(我当时没试过这种方式写样式)
注意:
① vue-count-to只能适用 Vue2,并不适用于Vue3;
② 对于Vue3还有个vue3-count-to,但是这个好像用不了,我当时试了,并没有加载出来,而且也没报错,还有待研究
另外除了这个vue-count-to这个插件组件外,还有个数字翻牌器,链接附上:
这个也可以实现,这个就类似个组件库,包比较大,还有其他的一些可视化效果(图表、动态换图、边框等等)
如果仅仅是实现这个数字滚动的话,还是使用vue-count-to方便一些,按需选择吧
总结
到此这篇关于Vue中使用vue-count-to(数字滚动插件)的文章就介绍到这了,更多相关Vue使用vue-count-to内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
