数字金额滚动动画

方法一、js

<template>
  <div>
    <div @click="this.numFun(0,520)">滚动</div>
   <div>{{amount}}</div>
  </div>
</<template>


<script>
export default {
  data () {
  return {
    amount: 0
  }
}
methods: {
    //金额变动动画
    numFun(startNum,maxNum) {
      var that = this
      let numText = startNum;
      let golb; // 为了清除requestAnimationFrame
      function numSlideFun(){ // 数字动画
          numText+=5; // 速度的计算可以为小数 。数字越大,滚动越快
          if(numText >= maxNum){
              numText = maxNum;
              cancelAnimationFrame(golb);
          }else {
              golb = requestAnimationFrame(numSlideFun);
          }
        that.amount=numText
        // console.log(numText)
      }
       numSlideFun(); // 调用数字动画
    }
  }
}

方法二、vue2下载插件

npm官网:https://www.npmjs.com/package/vue-count-to

npm install vue-count-to
<template>
   <div class="count-to">
     <div>
       <CountTo :startVal='startVal' :endVal='endVal' :duration='duration' />
     </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>
.count-to span {
  font-size: 30px;
}
.count-to > div:nth-of-type(1) > span {
  color: red;
}
</style>

方法三、jQuery

<body>
	<div class="num">
       <p style="color: #FFAC36;"><i>1997</i><span></span></p>
	</div>
</body>
<script src="js/numscroll.js"></script>//引入numscroll.js
<script type="text/javascript">
$(document).ready(function () {
	$(".num p i").numScroll({
		time:20000
	});
})
</script>

numscroll.js

/*!
 * jquery.numscroll.js -- 数字滚动累加动画插件  (Digital rolling cumulative animation)
 * version 1.0.0
 * 2018-09-22
 * author: KevinTseng < 921435247@qq.com@qq.com >
 * 文档:  https://github.com/chaorenzeng/jquery.numscroll.js.git
 * QQ交流群: 739574382
 */

(function($) {
    
    function isInt(num) {
        //作用:是否为整数
        //返回:true是 false否
        var res = false;
        try {
            if(String(num).indexOf(".") == -1 && String(num).indexOf(",") == -1) {
                res = parseInt(num) % 1 === 0 ? true : false;
            }
        } catch(e) {
            res = false;
        }
        return res;
    }

    function isFloat(num) {
        //作用:是否为小数
        //返回:小数位数(-1不是小数)
        var res = -1;
        try {
            if(String(num).indexOf(".") != -1) {
                var index = String(num).indexOf(".") + 1; //获取小数点的位置
                var count = String(num).length - index; //获取小数点后的个数
                if(index > 0) {
                    res = count;
                }
            }
        } catch(e) {
            res = -1;
        }
        return res;
    }

    $.fn.numScroll = function(options) {
        
        var settings = $.extend({
            'time': 1500,
            'delay': 0
        }, options);
        
        return this.each(function() {
            var $this = $(this);
            var $settings = settings;
            
            var num = $this.attr("data-num") || $this.text(); //实际值
            var temp = 0; //初始值
            $this.text(temp);
            var numIsInt = isInt(num);
            var numIsFloat = isFloat(num);
            var step = (num / $settings.time) * 10; //步长
            
            setTimeout(function() {
                var numScroll = setInterval(function() {
                    if(numIsInt) {
                        $this.text(Math.floor(temp));
                    } else if(numIsFloat != -1) {
                        $this.text(temp.toFixed(numIsFloat));
                    } else {
                        $this.text(num);
                        clearInterval(numScroll);
                        return;
                    }
                    temp += step;
                    if(temp > num) {
                        $this.text(num);
                        clearInterval(numScroll);
                    }
                }, 1);
            }, $settings.delay);
            
        });
    };

})(jQuery);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值