微信小程序通過(guò)自定義animate-numbers組件實(shí)現(xiàn)進(jìn)入頁(yè)面時(shí)數(shù)字跳動(dòng)效果
微信小程序中實(shí)現(xiàn)進(jìn)入頁(yè)面時(shí)數(shù)字跳動(dòng)效果
1. 組件定義,新建animate-numbers組件
1.1 index.js
// components/animate-numbers/index.js
Component({
properties: {
number: {
type: Number,
value: 0
},
duration: {
type: Number,
value: 1000
}
},
data: {
displayNumber: 0,
animationFrameId: null
},
observers: {
'number': function (newNumber) {
this.animateNumber(newNumber);
}
},
methods: {
animateNumber(targetNumber) {
const start = this.data.displayNumber;//舊值
const end = targetNumber;//新值
const duration = this.properties.duration;
const increment = (end - start) / (duration / 16); // 假設(shè)每秒60幀,每幀間隔約16ms
let current = start;
if(this.data.animationFrameId){
clearInterval(this.data.animationFrameId);
}
const animate = () => {
current += increment;
if ((increment > 0 && current >= end) || (increment < 0 && current <= end)) {
clearInterval(this.data.animationFrameId);
this.setData({ displayNumber: end });
} else {
this.setData({ displayNumber: Math.round(current) });
}
};
this.data.animationFrameId = setInterval(animate, 16);
}
},
// 組件被移除時(shí)清除定時(shí)器
detached() {
clearInterval(this.data.animationFrameId);
}
});1.2 wxml
<view>{{displayNumber}}</view>1.3 wxss
page {
font-size: 48rpx;
font-weight: bold;
}2. 使用組件
"animate-numbers": "../../../components/animate-numbers/index"
<animate-numbers number="{{attendanceInfo.month_avg_days}}" duration="1000"/>到此這篇關(guān)于微信小程序中實(shí)現(xiàn)進(jìn)入頁(yè)面時(shí)數(shù)字跳動(dòng)效果(自定義animate-numbers組件)的文章就介紹到這了,更多相關(guān)小程序數(shù)字跳動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Javascript 設(shè)計(jì)模式(二) 閉包
本來(lái)應(yīng)該是第二章,接口,但因?yàn)殚]包實(shí)在不懂,所以先看看閉包2010-05-05
微信小程序webview實(shí)現(xiàn)長(zhǎng)按點(diǎn)擊識(shí)別二維碼功能示例
這篇文章主要介紹了微信小程序webview實(shí)現(xiàn)長(zhǎng)按點(diǎn)擊識(shí)別二維碼功能,結(jié)合實(shí)例形式分析了webview二維碼識(shí)別相關(guān)操作技巧,需要的朋友可以參考下2019-01-01
JS使用插件cryptojs進(jìn)行加密解密數(shù)據(jù)實(shí)例
這篇文章主要介紹了JS使用插件cryptojs進(jìn)行加密解密數(shù)據(jù),結(jié)合完整實(shí)例形式分析了javascript基于加密插件實(shí)現(xiàn)加密解密功能的相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
javascript中style.left和offsetLeft的用法說(shuō)明
本篇文章主要是對(duì)javascript中style.left和offsetLeft的用法進(jìn)行了說(shuō)明介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-03-03
javascript里絕對(duì)用的上的字符分割函數(shù)總結(jié)
本節(jié)主要介紹了javascript里比較實(shí)用的字符分割函數(shù)的使用,需要的朋友可以參考下2014-07-07
TypeScript數(shù)組去重的20種實(shí)現(xiàn)方式
文章主要介紹了使用TypeScript實(shí)現(xiàn)數(shù)組去重的20種方法,從基礎(chǔ)循環(huán)、內(nèi)置數(shù)組方法、集合容器、排序后去重和遞歸與特殊等五個(gè)策略進(jìn)行分類,并詳細(xì)講解了每種方法的實(shí)現(xiàn)原理、適用場(chǎng)景以及性能特點(diǎn),此外,還提供了選擇不同去重方法的建議和實(shí)際項(xiàng)目中的應(yīng)用示例2026-05-05
js項(xiàng)目中雙向數(shù)據(jù)綁定的簡(jiǎn)單實(shí)現(xiàn)方法
雙向數(shù)據(jù)綁定指的就是,綁定對(duì)象屬性的改變到用戶界面的變化的能力,反之亦然,下面這篇文章主要給大家介紹了關(guān)于js項(xiàng)目中雙向數(shù)據(jù)綁定的簡(jiǎn)單實(shí)現(xiàn)方法,需要的朋友可以參考下2021-08-08
Js和JQuery獲取鼠標(biāo)指針坐標(biāo)的實(shí)現(xiàn)代碼分享
這篇文章主要介紹了Js和JQuery獲取鼠標(biāo)指針坐標(biāo)的實(shí)現(xiàn)代碼分享,本文直接給出實(shí)現(xiàn)的代碼,需要的朋友可以參考下2015-05-05
javascript垃圾收集機(jī)制與內(nèi)存泄漏詳細(xì)解析
本文是對(duì)javascript中的垃圾收集機(jī)制與內(nèi)存泄漏進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-11-11

