最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

在微信小程序中如何使用数字滚动插件

来源:动视网 责编:小采 时间:2020-11-27 19:39:34
文档

在微信小程序中如何使用数字滚动插件

在微信小程序中如何使用数字滚动插件:这篇文章主要介绍了微信小程序数字滚动插件的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下用es6语法方式写了个微信小程序小插件–数字滚动;效果图:wxml页面布局代码:<!--pages/main/index.wxml--><view class
推荐度:
导读在微信小程序中如何使用数字滚动插件:这篇文章主要介绍了微信小程序数字滚动插件的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下用es6语法方式写了个微信小程序小插件–数字滚动;效果图:wxml页面布局代码:<!--pages/main/index.wxml--><view class
 这篇文章主要介绍了微信小程序数字滚动插件的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

用es6语法方式写了个微信小程序小插件–数字滚动;

效果图:

wxml页面布局代码:

<!--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>

index.js调用NumberAnimate.js

// pages/main/index.jsimport 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:" 完成了"
 });
 }
 });
 }})

NumberAnimate.js代码:

/**
 * 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;

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

在zTree树插件中如何实现全国五级地区点击后加载

在webpack中如何使用ECharts?

JavaScript中Object基础内部方法图(图文教程)

文档

在微信小程序中如何使用数字滚动插件

在微信小程序中如何使用数字滚动插件:这篇文章主要介绍了微信小程序数字滚动插件的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下用es6语法方式写了个微信小程序小插件–数字滚动;效果图:wxml页面布局代码:<!--pages/main/index.wxml--><view class
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top