CountUp.js实现数字滚动增值效果实现数字滚动增值效果
主要为大家详细介绍了CountUp.js实现数字滚动增值效果,文中示例代码介绍的非常详细,具有一定的参考价
值,感兴趣的小伙伴们可以参考一下
数据改动的时候,countUp.js进行数值滚动增加的动态效果,供大家参考,具体内容如下
这是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)