数字累加,动态效果

数字累加,动态效果

2021-03-16
¥ 0 终身VIP免费 升级终身VIP
下载不了?请联系网站客服提交链接错误!
增值服务: 安装指导 环境配置 二次开发 网站建设 模板修改 源码安装 LOGO设计 图片设计
数字累加,动态效果
最近更新 2021年03月16日
资源编号 18200

数字累加,动态效果

郑重承诺丨总裁主题提供安全交易、信息保真!
增值服务:
安装指导
环境配置
二次开发
网站建设
模板修改
源码安装
¥ 0 (终身VIP免费 升级终身VIP开通VIP尊享优惠特权
立即下载 升级会员 最新活动
详情介绍

数字累加,动态效果

数字累加,动态效果

1、wxml代码

1
2
3
4
5
6
7
8
9
<!–pages/main/index.wxml–>
<view class=”animate-number”>
    <view class=”num num1″>{{num1}}{{num1Complete}}</view>
    <view class=”num num2″>{{num2}}{{num2Complete}}</view>
    <view class=”num num3″>{{num3}}{{num3Complete}}</view>
    <view class=”btn-box”>
        <button bindtap=”animate”  type=”primary” class=”button”>click me</button>
    </view>
</view>

2、index.js代码:

01
02
03
04
05
06
07
08
09
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
// pages/main/index.js
import NumberAnimate from “../../utils/NumberAnimate”;
Page({
  data:{
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
  },
  onReady:function(){
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  //调用NumberAnimate.js中NumberAnimate实例化对象,测试3种效果
 animate:function(){
   this.setData({
     num1:”,
     num2:”,
     num3:”,
     num1Complete:”,
     num2Complete:”,
     num3Complete:”
   });
    let num1 = 18362.856;
    let n1 = new NumberAnimate({
        from:num1,//开始时的数字
        speed:2000,// 总时间
        refreshTime:100,//  刷新一次的时间
        decimals:3,//小数点后的位数
        onUpdate:()=>{//更新回调函数
          this.setData({
            num1:n1.tempValue
          });
        },
        onComplete:()=>{//完成回调函数
            this.setData({
              num1Complete:” 完成了”
            });
        }
    });
    let num2 = 13388;
    let n2 = new NumberAnimate({
        from:num2,
        speed:1500,
        decimals:0,
        refreshTime:100,
        onUpdate:()=>{
          this.setData({
            num2:n2.tempValue
          });
        },
        onComplete:()=>{
            this.setData({
              num2Complete:” 完成了”
            });
        }
    });
    let num3 = 2123655255888.86;
    let n3 = new NumberAnimate({
        from:num3,
        speed:2000,
        refreshTime:100,
        decimals:2,
        onUpdate:()=>{
          this.setData({
            num3:n3.tempValue
          });
        },
        onComplete:()=>{
            this.setData({
              num3Complete:” 完成了”
            });
        }
    });
 }
})

3、NumberAnimate.js代码:

01
02
03
04
05
06
07
08
09
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
/**
 * Created by wangyy on 2016/12/26.
 */
‘use strict’;
class NumberAnimate {
    constructor(opt) {
        let def = {
            from:50,//开始时的数字
            speed:2000,// 总时间
            refreshTime:100,// 刷新一次的时间
            decimals:2,// 小数点后的位数
            onUpdate:function(){}, // 更新时回调函数
            onComplete:function(){} // 完成时回调函数
        }
        this.tempValue = 0;//累加变量值
        this.opt = Object.assign(def,opt);//assign传入配置参数
        this.loopCount = 0;//循环次数计数
        this.loops = Math.ceil(this.opt.speed/this.opt.refreshTime);//数字累加次数
        this.increment = (this.opt.from/this.loops);//每次累加的值
        this.interval = null;//计时器对象
        this.init();
    }
    init(){
        this.interval = setInterval(()=>{this.updateTimer()},this.opt.refreshTime);
    }
    updateTimer(){
        this.loopCount++;
        this.tempValue = this.formatFloat(this.tempValue,this.increment).toFixed(this.opt.decimals);
        if(this.loopCount >= this.loops){
            clearInterval(this.interval);
            this.tempValue = this.opt.from;
            this.opt.onComplete();
        }
        this.opt.onUpdate();
    }
    //解决0.1+0.2不等于0.3的小数累加精度问题
    formatFloat(num1, num2) {
        let baseNum, baseNum1, baseNum2;
        try {
            baseNum1 = num1.toString().split(“.”)[1].length;
        } catch (e) {
            baseNum1 = 0;
        }
        try {
            baseNum2 = num2.toString().split(“.”)[1].length;
        } catch (e) {
            baseNum2 = 0;
        }
        baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
        return (num1 * baseNum + num2 * baseNum) / baseNum;
    };
}
export default  NumberAnimate;
资源下载此资源仅限注册用户下载,请先
客服QQ:3482249445
收藏 (0) 打赏

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

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

所有文章为演示数据,不提供下载地址,版权归原作者所有,仅提供演示效果!

小程序网 小程序 数字累加,动态效果 https://www.10000ii.com/18200.html

下一篇: AQMH
常见问题
  • 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用
查看详情

相关文章

数字累加,动态效果-海报

分享本文封面