CountUp.js實(shí)現(xiàn)數(shù)字滾動(dòng)增值效果
數(shù)據(jù)改動(dòng)的時(shí)候,countUp.js進(jìn)行數(shù)值滾動(dòng)增加的動(dòng)態(tài)效果,供大家參考,具體內(nèi)容如下
這是js文件
// target = id of html element or var of previously selected html element where counting occurs
// startVal = the value you want to begin at
// endVal = the value you want to arrive at
// decimals = number of decimal places, default 0
// duration = duration of animation in seconds, default 2
// options = optional object of options (see below)
var CountUp = function (target, startVal, endVal, decimals, duration, options) {
var self = this
self.version = function () { return '1.9.3' }
// default options
self.options = {
useEasing: true, // toggle easing
useGrouping: true, // 1,000,000 vs 1000000
separator: ',', // character to use as a separator
decimal: '.', // character to use as a decimal
easingFn: easeOutExpo, // optional custom easing function, default is Robert Penner's easeOutExpo
formattingFn: formatNumber, // optional custom formatting function, default is formatNumber above
prefix: '', // optional text before the result
suffix: '', // optional text after the result
numerals: [] // optionally pass an array of custom numerals for 0-9
}
// extend default options with passed options object
if (options && typeof options === 'object') {
for (var key in self.options) {
if (options.hasOwnProperty(key) && options[key] !== null) {
self.options[key] = options[key]
}
}
}
if (self.options.separator === '') {
self.options.useGrouping = false
} else {
// ensure the separator is a string (formatNumber assumes this)
self.options.separator = '' + self.options.separator
}
// make sure requestAnimationFrame and cancelAnimationFrame are defined
// polyfill for browsers without native support
// by Opera engineer Erik Möller
var lastTime = 0
var vendors = ['webkit', 'moz', 'ms', 'o']
for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame']
window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] || window[vendors[x] + 'CancelRequestAnimationFrame']
}
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = function (callback, element) {
var currTime = new Date().getTime()
var timeToCall = Math.max(0, 16 - (currTime - lastTime))
var tesult = currTime + timeToCall
var id = window.setTimeout(function () { callback(tesult) }, timeToCall)
lastTime = currTime + timeToCall
return id
}
}
if (!window.cancelAnimationFrame) {
window.cancelAnimationFrame = function (id) {
clearTimeout(id)
}
}
function formatNumber (num) {
var neg = (num < 0)
var x, x1, x2, x3, i, len
num = Math.abs(num).toFixed(self.decimals)
num += ''
x = num.split('.')
x1 = x[0]
x2 = x.length > 1 ? self.options.decimal + x[1] : ''
if (self.options.useGrouping) {
x3 = ''
for (i = 0, len = x1.length; i < len; ++i) {
if (i !== 0 && ((i % 3) === 0)) {
x3 = self.options.separator + x3
}
x3 = x1[len - i - 1] + x3
}
x1 = x3
}
// optional numeral substitution
if (self.options.numerals.length) {
x1 = x1.replace(/[0-9]/g, function (w) {
return self.options.numerals[+w]
})
x2 = x2.replace(/[0-9]/g, function (w) {
return self.options.numerals[+w]
})
}
return (neg ? '-' : '') + self.options.prefix + x1 + x2 + self.options.suffix
}
// Robert Penner's easeOutExpo
function easeOutExpo (t, b, c, d) {
return c * (-Math.pow(2, -10 * t / d) + 1) * 1024 / 1023 + b
}
function ensureNumber (n) {
return (typeof n === 'number' && !isNaN(n))
}
self.initialize = function () {
if (self.initialized) return true
self.error = ''
self.d = (typeof target === 'string') ? document.getElementById(target) : target
if (!self.d) {
self.error = '[CountUp] target is null or undefined'
return false
}
self.startVal = Number(startVal)
self.endVal = Number(endVal)
// error checks
if (ensureNumber(self.startVal) && ensureNumber(self.endVal)) {
self.decimals = Math.max(0, decimals || 0)
self.dec = Math.pow(10, self.decimals)
self.duration = Number(duration) * 1000 || 2000
self.countDown = (self.startVal > self.endVal)
self.frameVal = self.startVal
self.initialized = true
return true
} else {
self.error = '[CountUp] startVal (' + startVal + ') or endVal (' + endVal + ') is not a number'
return false
}
}
// Print value to target
self.printValue = function (value) {
var result = self.options.formattingFn(value)
if (self.d.tagName === 'INPUT') {
this.d.value = result
} else if (self.d.tagName === 'text' || self.d.tagName === 'tspan') {
this.d.textContent = result
} else {
this.d.innerHTML = result
}
}
self.count = function (timestamp) {
if (!self.startTime) { self.startTime = timestamp }
self.timestamp = timestamp
var progress = timestamp - self.startTime
self.remaining = self.duration - progress
// to ease or not to ease
if (self.options.useEasing) {
if (self.countDown) {
self.frameVal = self.startVal - self.options.easingFn(progress, 0, self.startVal - self.endVal, self.duration)
} else {
self.frameVal = self.options.easingFn(progress, self.startVal, self.endVal - self.startVal, self.duration)
}
} else {
if (self.countDown) {
self.frameVal = self.startVal - ((self.startVal - self.endVal) * (progress / self.duration))
} else {
self.frameVal = self.startVal + (self.endVal - self.startVal) * (progress / self.duration)
}
}
// don't go past endVal since progress can exceed duration in the last frame
if (self.countDown) {
self.frameVal = (self.frameVal < self.endVal) ? self.endVal : self.frameVal
} else {
self.frameVal = (self.frameVal > self.endVal) ? self.endVal : self.frameVal
}
// decimal
self.frameVal = Math.round(self.frameVal * self.dec) / self.dec
// format and print value
self.printValue(self.frameVal)
// whether to continue
if (progress < self.duration) {
self.rAF = requestAnimationFrame(self.count)
} else {
if (self.callback) self.callback()
}
}
// start your animation
self.start = function (callback) {
if (!self.initialize()) return
self.callback = callback
self.rAF = requestAnimationFrame(self.count)
}
// toggles pause/resume animation
self.pauseResume = function () {
if (!self.paused) {
self.paused = true
cancelAnimationFrame(self.rAF)
} else {
self.paused = false
delete self.startTime
self.duration = self.remaining
self.startVal = self.frameVal
requestAnimationFrame(self.count)
}
}
// reset to startVal so animation can be run again
self.reset = function () {
self.paused = false
delete self.startTime
self.initialized = false
if (self.initialize()) {
cancelAnimationFrame(self.rAF)
self.printValue(self.startVal)
}
}
// pass a new endVal and start animation
self.update = function (newEndVal) {
if (!self.initialize()) return
newEndVal = Number(newEndVal)
if (!ensureNumber(newEndVal)) {
self.error = '[CountUp] update() - new endVal is not a number: ' + newEndVal
return
}
self.error = ''
if (newEndVal === self.frameVal) return
cancelAnimationFrame(self.rAF)
self.paused = false
delete self.startTime
self.startVal = self.frameVal
self.endVal = newEndVal
self.countDown = (self.startVal > self.endVal)
self.rAF = requestAnimationFrame(self.count)
}
// format startVal on initialization
if (self.initialize()) self.printValue(self.startVal)
}
module.exports = CountUp
index.html文件中需要進(jìn)入該插件,和jq插件,再進(jìn)行new實(shí)例化
var options = {
useEasing: true, // 使用緩和效果
useGrouping: true, // 使用分組效果
separator: ',', // 分離器,數(shù)據(jù)夠三位,例如100,000
decimal: '.', // 小數(shù)點(diǎn)分割,例如:10.00
prefix: '', // 第一位默認(rèn)數(shù)字,例如:¥
suffix: '' // 最后一位默認(rèn)數(shù)字,例如:元,美元
}
// new CountUp(target, startVal, endVal, decimals, duration, options)
// target = 目標(biāo)元素的 ID
// startVal = 開始值
// endVal = 結(jié)束值
// decimals = 小數(shù)位數(shù) 默認(rèn)值是0
// duration = 動(dòng)畫延遲秒數(shù),默認(rèn)值是2;
// options = optional object of options (see below)
var demo = new CountUp('extractionMoney', 0, data.balanceAmount, 2, 0.5, options)
if (!demo.error) {
demo.start()
} else {
console.error(demo.error)
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js解決彈窗問題實(shí)現(xiàn)班級(jí)跳轉(zhuǎn)DIV示例
本文為大家介紹下js如何解決彈窗問題實(shí)現(xiàn)班級(jí)跳轉(zhuǎn)DIV,具體示例如下,感興趣的朋友可以參考下2014-01-01
JavaScript中Array數(shù)組常用方法(附上相應(yīng)的用法及示例)
這篇文章主要給大家介紹了關(guān)于JavaScript中Array數(shù)組常用方法,文中附上相應(yīng)的用法及示例,需要的朋友可以參考下2024-01-01
微信小程序 高德地圖路線規(guī)劃實(shí)現(xiàn)過程詳解
這篇文章主要介紹了微信小程序 路線規(guī)劃實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
JavaScript評(píng)論點(diǎn)贊功能的實(shí)現(xiàn)方法
通過分析評(píng)論功能的邏輯關(guān)系,學(xué)會(huì)如何使用JavaScript實(shí)現(xiàn)評(píng)論、回復(fù)、點(diǎn)贊等各種功能。這篇文章主要介紹了JavaScript評(píng)論點(diǎn)贊功能的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-03-03
JS字符串統(tǒng)計(jì)操作示例【遍歷,截取,輸出,計(jì)算】
這篇文章主要介紹了JS字符串統(tǒng)計(jì)操作,結(jié)合實(shí)例形式分析了javascript字符串的遍歷,截取,輸出,計(jì)算等相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2017-03-03
JS優(yōu)化與惰性載入函數(shù)實(shí)例分析
這篇文章主要介紹了JS優(yōu)化與惰性載入函數(shù),結(jié)合具體實(shí)例形式分析了JS惰性載入的原理、實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-04-04
javascript實(shí)現(xiàn)依次輸入input自動(dòng)定焦
這篇文章主要介紹了javascript實(shí)現(xiàn)依次輸入input自動(dòng)定焦的方法及示例代碼,非常實(shí)用,這里推薦給小伙伴們2014-12-12
JavaScript中好用的數(shù)組對(duì)象排序方法分享
在日常工作中,我們經(jīng)常需要對(duì)數(shù)組對(duì)象進(jìn)行排序,尤其是在處理數(shù)據(jù)可視化需求中。本文將介紹一些簡(jiǎn)單而又實(shí)用的方法,幫助你實(shí)現(xiàn)對(duì)數(shù)組對(duì)象的某幾個(gè) key 進(jìn)行排序2023-05-05

