Last active
June 28, 2023 22:03
-
-
Save inorganik/b63dbe5b3810ff2c0175aee4670a4732 to your computer and use it in GitHub Desktop.
A CountUp extension for jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Dependency: CountUp.js: https://github.com/inorganik/CountUp.js | |
(function ($) { | |
$.fn.countup = function (params) { | |
// make sure dependency is present | |
if (typeof CountUp !== 'function') { | |
console.error('countUp.js is a required dependency of countUp-jquery.js.'); | |
return; | |
} | |
var defaults = { | |
startVal: 0, | |
decimalPlaces: 0, | |
duration: 2, | |
}; | |
if (typeof params === 'number') { | |
defaults.endVal = params; | |
} | |
else if (typeof params === 'object') { | |
$.extend(defaults, params); | |
} | |
else { | |
console.error('countUp-jquery requires its argument to be either an object or number'); | |
return; | |
} | |
this.each(function (i, elem) { | |
var countUp = new CountUp(elem, defaults.endVal, defaults); | |
countUp.start(); | |
}); | |
return this; | |
}; | |
}(jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment