How to use digital scrolling plug-in in WeChat mini program
This article mainly introduces how to use the digital scrolling plug-in of the WeChat applet. It has a certain reference value. Interested friends can refer to it.
Write a WeChat applet using es6 syntax. Small plug-in – digital scrolling;
Rendering:
wxml page layout code:
<!--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 calls 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 code:
/** * 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;
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
How to use ECharts in webpack ?
Basic Internal Method Diagram of Object in JavaScript (Graphic Tutorial)
The above is the detailed content of How to use digital scrolling plug-in in WeChat mini program. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.
