vue如何实现数字滚动增加效果?代码示例


本文摘自PHP中文网,作者青灯夜游,侵删。

项目中需要做数字滚动增加的效果,一开始很懵,研究了一下原理,发现很简单,贴出来分享一下 ^_^

数字滚动组件:

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

<template>

<p class="number-grow-warp">

  <span ref="numberGrow" :data-time="time" class="number-grow" :data-value="value">0</span>

  </p>

</template>

 

<script> export default {

  props: {

    time: {

      type: Number,

      default: 2

    },

    value: {

      type: Number,

      default: 720000

    }

  },

  methods: {

    numberGrow (ele) {

      let _this = this

 

      let step = (_this.value * 10) / (_this.time * 1000)

      let current = 0

      let start = 0

      let t = setInterval(function () {

        start += step

        if (start > _this.value) {

          clearInterval(t)

          start = _this.value

          t = null

        }

        if (current === start) {

          return

        }

        current = start

        ele.innerHTML = current.toString().replace(/(d)(?=(?:d{3}[+]?)+$)/g, '$1,')

      }, 10)

    }

  },

  mounted () {

    this.numberGrow(this.$refs.numberGrow)

  }

} </script>

 

<style> .number-grow-warp{

  transform: translateZ(0);

}

.number-grow {

  font-family: Arial-BoldMT;

  font-size: 64px;

  color: #ffaf00;

  letter-spacing: 2.67px;

  margin:110px 0 20px;

  display: block;

  line-height:64px;

} </style>

调用:

1

<NumberGrow :value="720000"></NumberGrow>

相关推荐:

阅读剩余部分

相关阅读 >>

分享es2019中值得收藏的8个有用功能

javascript中的垃圾回收和内存泄漏

html获取javascript变量值的方法有哪些

vue.js属于前端框架么

js中闭包的概念

javascript怎么累加

javascript中类型检查:typeof和instanceof操作符的区别

快速入门bootstrapvue

vue响应式原理及依赖收集的介绍 (附代码)

javascript怎样修改div内容

更多相关阅读请进入《javascript》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...