Research
Security News
Kill Switch Hidden in npm Packages Typosquatting Chalk and Chokidar
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
countup.js
Advanced tools
countup.js is a lightweight JavaScript library that allows you to create animated counting numbers. It is useful for creating dynamic and engaging number displays in web applications, such as statistics, counters, and other numerical data visualizations.
Basic Count Up
This feature allows you to animate a number counting up from zero to a specified value. In this example, the number will count up to 1000 and display in the element with the ID 'targetElement'.
const countUp = new CountUp('targetElement', 1000);
if (!countUp.error) {
countUp.start();
} else {
console.error(countUp.error);
}
Custom Start and End Values
This feature allows you to specify custom start and end values for the count up animation. In this example, the number will count up from 500 to 2000.
const countUp = new CountUp('targetElement', 2000, { startVal: 500 });
if (!countUp.error) {
countUp.start();
} else {
console.error(countUp.error);
}
Formatting Options
This feature allows you to format the number with options such as decimal places, prefixes, and suffixes. In this example, the number will be formatted with two decimal places and display as currency (e.g., $1000.00 USD).
const countUp = new CountUp('targetElement', 1000, { decimalPlaces: 2, prefix: '$', suffix: ' USD' });
if (!countUp.error) {
countUp.start();
} else {
console.error(countUp.error);
}
Easing Functions
This feature allows you to apply custom easing functions to the count up animation. In this example, a quadratic easing function is used to create a smooth animation effect.
const countUp = new CountUp('targetElement', 1000, { easingFn: function (t, b, c, d) { return c * (t /= d) * t + b; } });
if (!countUp.error) {
countUp.start();
} else {
console.error(countUp.error);
}
Odometer is a smooth, themeable, and easy-to-use JavaScript library for transitioning numbers. It provides a more visually appealing way to animate numbers compared to countup.js, with various themes and styles that mimic the look of an odometer.
jquery.counterup is a jQuery plugin that animates a number from zero to a specified value. It is similar to countup.js but requires jQuery as a dependency, making it less lightweight. It is suitable for projects that already use jQuery.
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.
Despite its name, CountUp can count in either direction, depending on the start and end values that you pass.
CountUp.js supports all browsers. MIT license.
Or tinker with CountUp in Stackblitz
enableScrollSpy
.countUp.min.js
for modern browsers or countUp.withPolyfill.min.js
for IE9 and older, and Opera mini.On npm as: countup.js
. You can import as a module or include the UMD script and access CountUp as a global. See detailed instructions below.
Params:
target: string | HTMLElement | HTMLInputElement
- id of html element, input, svg text element, or DOM element reference where counting occursendVal: number
- the value you want to arrive atoptions?: CountUpOptions
- optional configuration object for fine-grain controlOptions (defaults in parentheses):
interface CountUpOptions {
startVal?: number; // number to start at (0)
decimalPlaces?: number; // number of decimal places (0)
duration?: number; // animation duration in seconds (2)
useGrouping?: boolean; // example: 1,000 vs 1000 (true)
useIndianSeparators?: boolean; // example: 1,00,000 vs 100,000 (false)
useEasing?: boolean; // ease animation (true)
smartEasingThreshold?: number; // smooth easing for large numbers above this if useEasing (999)
smartEasingAmount?: number; // amount to be eased for numbers above threshold (333)
separator?: string; // grouping separator (',')
decimal?: string; // decimal ('.')
// easingFn: easing function for animation (easeOutExpo)
easingFn?: (t: number, b: number, c: number, d: number) => number;
formattingFn?: (n: number) => string; // this function formats result
prefix?: string; // text prepended to result
suffix?: string; // text appended to result
numerals?: string[]; // numeral glyph substitution
enableScrollSpy?: boolean; // start animation when target is in view
scrollSpyDelay?: number; // delay (ms) after target comes into view
scrollSpyOnce?: boolean; // run only once
}
const countUp = new CountUp('targetId', 5234);
if (!countUp.error) {
countUp.start();
} else {
console.error(countUp.error);
}
Pass options:
const countUp = new CountUp('targetId', 5234, options);
with optional callback:
countUp.start(someMethodToCallOnComplete);
// or an anonymous function
countUp.start(() => console.log('Complete!'));
Other methods:
Toggle pause/resume:
countUp.pauseResume();
Reset the animation:
countUp.reset();
Update the end value and animate:
countUp.update(989);
Use the scroll spy option to animate when the element is scrolled into view. When using scroll spy, just initialize CountUp but do not call start();
const countUp = new CountUp('targetId', 989, { enableScrollSpy: true });
Troubleshooting scroll spy
CountUp checks the scroll position as soon as it's initialized. So if you initialize it before the DOM renders and your target element is in view before any scrolling, you'll need to re-check the scroll position after the page renders:
// after DOM has rendered
countUp.handleScroll();
CountUp is distributed as an ES6 module because it is the most standardized and most widely compatible module for browsers, though a UMD module is also included.
For the examples below, first install CountUp. This will give you the latest:
npm i countup.js
This is what I used in the demo. Checkout index.html and demo.js.
main.js:
import { CountUp } from './js/countUp.min.js';
window.onload = function() {
var countUp = new CountUp('target', 2000);
countUp.start();
}
Include in your html. Notice the type
attribute:
<script src="./main.js" type="module"></script>
To support IE and legacy browsers, use the nomodule
script tag to include separate scripts that don't use the module syntax:
<script nomodule src="js/countUp.umd.js"></script>
<script nomodule src="js/main-for-legacy.js"></script>
To run module-enabled scripts locally, you'll need a simple local server setup like this (test the demo locally by running npm run serve
) because otherwise you may see a CORS error when your browser tries to load the script as a module.
Import from the package, instead of the file location:
import { CountUp } from 'countup.js';
CountUp is also wrapped as a UMD module in ./dist/countUp.umd.js
and it exposes CountUp as a global variable on the window scope. To use it, include countUp.umd.js
in a script tag, and invoke it like so:
var numAnim = new countUp.CountUp('myTarget', 2000);
numAnim.start()
Before you make a pull request, please be sure to follow these instructions:
src/countUp.ts
npm run lint
npm t
npm start
then check the demo to make sure it counts.FAQs
Animates a numerical value by counting to it
The npm package countup.js receives a total of 362,288 weekly downloads. As such, countup.js popularity was classified as popular.
We found that countup.js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.