微信小程序通過自定義animate-numbers組件實(shí)現(xiàn)進(jìn)入頁面時(shí)數(shù)字跳動(dòng)效果
微信小程序中實(shí)現(xiàn)進(jìn)入頁面時(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)入頁面時(shí)數(shù)字跳動(dòng)效果(自定義animate-numbers組件)的文章就介紹到這了,更多相關(guān)小程序數(shù)字跳動(dòng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js數(shù)組常用操作方法小結(jié)(增加,刪除,合并,分割等)
這篇文章主要介紹了js數(shù)組常用操作方法,結(jié)合實(shí)例總結(jié)了javascript數(shù)組的增加、刪除、合并、分割等操作技巧,需要的朋友可以參考下2016-08-08JS實(shí)現(xiàn)的簡單折疊展開動(dòng)畫效果示例
這篇文章主要介紹了JS實(shí)現(xiàn)的簡單折疊展開動(dòng)畫效果,可實(shí)現(xiàn)類似百度頁面分享按鈕一樣的折疊展開動(dòng)畫效果,涉及javascript頁面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-04-04javascript實(shí)現(xiàn)數(shù)字配對游戲的實(shí)例講解
下面小編就為大家分享一篇javascript實(shí)現(xiàn)數(shù)字配對游戲的實(shí)例講解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12javascript實(shí)現(xiàn)倒計(jì)時(shí)N秒后網(wǎng)頁自動(dòng)跳轉(zhuǎn)代碼
這篇文章主要介紹了javascript實(shí)現(xiàn)倒計(jì)時(shí)N秒后網(wǎng)頁自動(dòng)跳轉(zhuǎn)代碼,非常的實(shí)用,這里推薦給大家。2014-12-12JavaScript原生節(jié)點(diǎn)操作小結(jié)
本文主要介紹了JavaScript原生節(jié)點(diǎn)操作的相關(guān)知識。具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01three.js實(shí)現(xiàn)3D影院的原理的代碼分析
本篇文章主要給大家講解了如何通過three.js實(shí)現(xiàn)3D影院的功能以及原理分析,需要的朋友參考一下吧。2017-12-12