|
1 | 1 | # CountUp.js |
2 | | -CountUp.js is a dependency-free, lightweight JavaScript "class" that can be used to quickly create animations that display numerical data in a more interesting way. |
| 2 | +CountUp.js is a dependency-free, lightweight Javascript class that can be used to quickly create animations that display numerical data in a more interesting way. |
3 | 3 |
|
4 | | -Despite its name, CountUp can count in either direction, depending on the `startVal` and `endVal` params that you pass. |
| 4 | +Despite its name, CountUp can count in either direction, depending on the start value and end value that you pass. |
5 | 5 |
|
6 | | -CountUp.js supports all browsers. |
| 6 | +CountUp.js supports all browsers. MIT license. |
7 | 7 |
|
8 | 8 | ## [Try the demo](http://inorganik.github.io/countUp.js) |
9 | 9 |
|
| 10 | +## New in 2.0.0 |
| 11 | + |
| 12 | +- Completely rewritten in **Typescript**! The distributed code is still Javascript. |
| 13 | +- **New** cleaner [method signature](#example). |
| 14 | +- Tests with **Jest**. As much code coverage as possible mocking requestAnimationFrame. |
| 15 | +- **Smart easing**: CountUp intelligently defers easing until it gets close enough to the end value for easing to be visually noticeable. Configureable in the [options](#options). |
| 16 | +- **Separate bundles** for with and without the requestAnimationFrame polyfill. Choose `countUp.min.js` for modern browsers or `countUp.withPolyfill.min.js` for IE9 and older, and Opera mini. |
| 17 | + |
10 | 18 | ## See Also |
11 | 19 |
|
12 | | -- **[CountUp.js Angular ^2 Module](https://github.com/inorganik/countUp.js-angular2)**  |
| 20 | +- **[CountUp.js Angular ^2 Module](https://github.com/inorganik/countUp.js-angular2)** |
13 | 21 | - **[CountUp.js Angular 1.x Module](https://github.com/inorganik/countUp.js-angular1)** |
14 | 22 | - **[CountUp.js React](https://github.com/glennreyes/react-countup)** |
15 | 23 | - **[CountUp.js Vue component wrapper](https://github.com/xlsdg/vue-countup-v2)** |
16 | 24 | - **[CountUp.js WordPress Plugin](https://wordpress.org/plugins/countup-js/)** |
17 | | - |
18 | | -## Installation |
19 | | - |
20 | | -Simply include the countUp.js file in your project or install via npm/yarn using the package name `countup.js`. |
21 | | - |
22 | | -Before making a pull request, please [read this](#contributing). MIT License. |
23 | | - |
24 | | -A jQuery version is also included, but needs to be included manually. |
| 25 | +- **[CountUp.js jQuery Plugin](https://gist.github.com/inorganik/b63dbe5b3810ff2c0175aee4670a4732)** |
25 | 26 |
|
26 | 27 | ## Usage: |
27 | | -Params: |
28 | | -- `target` = id of html element, input, svg text element, or var of previously selected element/input where counting occurs |
29 | | -- `startVal` = the value you want to begin at |
30 | | -- `endVal` = the value you want to arrive at |
31 | | -- `decimals` = (optional) number of decimal places in number, default 0 |
32 | | -- `duration` = (optional) duration in seconds, default 2 |
33 | | -- `options` = (optional, see demo) formatting/easing options object |
34 | 28 |
|
35 | | -Decimals, duration, and options can be left out to use the default values. |
| 29 | +**On npm**: `countup.js` |
| 30 | + |
| 31 | +**Params**: |
| 32 | +- `target: string | HTMLElement | HTMLInputElement` - id of html element, input, svg text element, or DOM element reference where counting occurs |
| 33 | +- `endVal: number` - the value you want to arrive at |
| 34 | +- `options?: CountUpOptions` - optional configuration object for fine-grain control |
| 35 | + |
| 36 | +**Options** (defaults in parentheses): <a name="options"></a> |
| 37 | + |
| 38 | +```ts |
| 39 | +interface CountUpOptions { |
| 40 | + startVal?: number; // number to start at (0) |
| 41 | + decimalPlaces?: number; // number of decimal places (0) |
| 42 | + duration?: number; // animation duration in seconds (2) |
| 43 | + useGrouping?: boolean; // example: 1,000 vs 1000 (true) |
| 44 | + useEasing?: boolean; // ease animation (true) |
| 45 | + smartEasingThreshold?: number; // smooth easing for large numbers above this if useEasing (999) |
| 46 | + smartEasingAmount?: number; // amount to be eased for numbers above threshold (333) |
| 47 | + separator?: string; // grouping separator (',') |
| 48 | + decimal?: string; // decimal ('.') |
| 49 | + // easingFn: easing function for animation (easeOutExpo) |
| 50 | + easingFn?: (t: number, b: number, c: number, d: number) => number; |
| 51 | + formattingFn?: (n: number) => string; // this function formats result |
| 52 | + prefix?: string; // text prepended to result |
| 53 | + suffix?: string; // text appended to result |
| 54 | + numerals?: string[]; // numeral glyph substitution |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +**Example usage**: <a name="example"></a> |
36 | 59 |
|
37 | 60 | ```js |
38 | | -var numAnim = new CountUp("SomeElementYouWantToAnimate", 24.02, 99.99); |
39 | | -if (!numAnim.error) { |
40 | | - numAnim.start(); |
| 61 | +const countUp = new CountUp('targetId', 5234); |
| 62 | +if (!countUp.error) { |
| 63 | + countUp.start(); |
41 | 64 | } else { |
42 | | - console.error(numAnim.error); |
| 65 | + console.error(countUp.error); |
43 | 66 | } |
44 | 67 | ``` |
45 | 68 |
|
| 69 | +Pass options: |
| 70 | +```js |
| 71 | +const countUp = new CountUp('targetId', 5234, options); |
| 72 | +``` |
| 73 | + |
46 | 74 | with optional callback: |
47 | 75 |
|
48 | 76 | ```js |
49 | | -numAnim.start(someMethodToCallOnComplete); |
| 77 | +countUp.start(someMethodToCallOnComplete); |
50 | 78 |
|
51 | 79 | // or an anonymous function |
52 | | -numAnim.start(function() { |
53 | | - // do something |
54 | | -}) |
| 80 | +countUp.start(() => console.log('Complete!')); |
55 | 81 | ``` |
56 | 82 |
|
57 | | -#### Other methods: |
| 83 | +**Other methods**: |
| 84 | + |
58 | 85 | Toggle pause/resume: |
59 | 86 |
|
60 | 87 | ```js |
61 | | -numAnim.pauseResume(); |
| 88 | +countUp.pauseResume(); |
62 | 89 | ``` |
63 | 90 |
|
64 | | -Reset an animation: |
| 91 | +Reset the animation: |
65 | 92 |
|
66 | 93 | ```js |
67 | | -numAnim.reset(); |
| 94 | +countUp.reset(); |
68 | 95 | ``` |
69 | 96 |
|
70 | 97 | Update the end value and animate: |
71 | 98 |
|
72 | 99 | ```js |
73 | | -var someValue = 1337; |
74 | | -numAnim.update(someValue); |
75 | | -``` |
76 | | - |
77 | | -#### Animating to large numbers |
78 | | -For large numbers, since CountUp has a long way to go in just a few seconds, the animation seems to abruptly stop. The solution is to subtract 100 from your `endVal`, then use the callback to invoke the `update` method which completes the animation with the same duration with a difference of only 100 to animate: |
79 | | -```js |
80 | | -var endVal = 9645.72; |
81 | | -var numAnim = new CountUp('targetElem', 0, endVal - 100, 2, duration/2); |
82 | | -numAnim.start(function() { |
83 | | - numAnim.update(endVal); |
84 | | -}); |
| 100 | +countUp.update(989); |
85 | 101 | ``` |
86 | 102 |
|
87 | 103 | ## Contributing <a name="contributing"></a> |
88 | 104 |
|
89 | 105 | Before you make a pull request, please be sure to follow these instructions: |
90 | 106 |
|
91 | | -1. Do your work on `countUp.js` and/or other files in the root directory. |
92 | | -2. In Terminal, `cd` to the `countUp.js` directory. |
93 | | -3. Run `npm i` |
94 | | -4. Run `npm run build`, which copies and minifies the .js files to the `dist` folder. |
| 107 | +1. Do your work on `src/countUp.ts` |
| 108 | +1. Test your work. Do manual tests on the demo in the browser and run `npm t` |
| 109 | +1. Run `npm run build`, which copies and minifies the .js files to the `dist` folder. |
0 commit comments