Velocity.js Animation Library
Velocity.js is a high-performance JavaScript animation library that provides a feature-rich and fast alternative to traditional methods like jQuery's `$.animate()` or basic CSS transitions. It enables complex UI motion design by optimizing DOM interactions to minimize layout thrashing and leveraging efficient JavaScript animation techniques. Key features include support for color animations, CSS transforms, loops, easings, SVG properties, and scrolling. Velocity.js offers an API that closely mirrors jQuery's `$.animate()`, allowing for seamless integration with or independent use from jQuery. The library is currently stable at version 2.0.6, released in September 2023, demonstrating ongoing maintenance and development. This version integrated the former `UI-Pack` animations directly into its core and introduced a Promise-based API for modern asynchronous control flow. It aims to deliver significantly smoother animations, especially on mobile devices, by focusing on performance at its core.
Common errors
-
ReferenceError: Velocity is not defined
cause The `Velocity` object was not correctly imported, the script was not loaded, or it was loaded in a way that doesn't expose it globally in the current scope (e.g., using a module bundler without proper configuration).fixFor ES Modules, use `import Velocity from 'velocity-animate';`. For CommonJS, use `const Velocity = require('velocity-animate');`. If using a script tag, ensure `velocity.js` is loaded before your animation code, and `Velocity` will be available globally. -
TypeError: (element).velocity is not a function
cause This error typically occurs when attempting to use Velocity.js with a jQuery-like chaining syntax (`$(element).velocity(...)`) but jQuery is either not loaded, or Velocity.js was not correctly integrated with jQuery, or you're trying to use chaining on a raw DOM element without the `Velocity(element, ...)` utility function.fixIf you intend to use jQuery, ensure jQuery is loaded *before* Velocity.js. If not using jQuery, animate raw DOM elements directly using `Velocity(element, properties, options);` rather than attempting to call `.velocity()` as a method on the element itself or a jQuery object.
Warnings
- breaking Velocity.js v2 introduced significant API changes compared to v1, including a re-written Sequences module and modifications to how properties like `display` and `visibility` are handled (now properties, not options).
- breaking In Velocity.js v2.0.6 and later, the `velocity.ui.js` module is no longer distributed separately; its core animations have been integrated directly into the main `velocity-animate` package.
- gotcha Velocity.js v2 primarily uses a Promise-based API for chaining animations and handling completion, a departure from the callback-centric approach of v1. While callbacks still exist, Promises are the idiomatic way to handle asynchronous flow.
- gotcha Mixing ES Modules (`import`) and CommonJS (`require`) in Node.js environments can lead to errors like 'Cannot use import statement outside a module' or 'require is not defined'.
- gotcha While Velocity.js can seamlessly integrate with jQuery, if you replace jQuery's `$.animate()` with `$.velocity()`, you should consider building a custom jQuery version without its animation module to reduce file size, as Velocity.js provides its own animation stack.
Install
-
npm install velocity-animate -
yarn add velocity-animate -
pnpm add velocity-animate
Imports
- Velocity
const Velocity = require('velocity-animate');import Velocity from 'velocity-animate';
- Velocity
import Velocity from 'velocity-animate';
const Velocity = require('velocity-animate'); - Velocity
// Global in browser after <script src="velocity.min.js"></script>
- Properties, Easing, ElementCallback
import type { Properties, Easing, ElementCallback } from 'velocity-animate';
Quickstart
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Velocity.js Quickstart</title>
<style>
#myElement {
width: 100px;
height: 100px;
background-color: steelblue;
position: absolute;
top: 50px;
left: 50px;
border-radius: 5px;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-family: sans-serif;
font-size: 1.2em;
}
</style>
</head>
<body>
<div id="myElement">Animate Me</div>
<script type="module">
import Velocity from 'velocity-animate';
const element = document.getElementById('myElement');
Velocity(element, {
translateX: '200px',
rotateZ: '360deg',
backgroundColor: '#ef5350'
}, {
duration: 1500,
easing: 'ease-in-out',
loop: 2,
delay: 500,
complete: () => {
console.log('Animation complete!');
// Chain another animation using Promises (V2 feature)
Velocity(element, {
scale: 0.8,
opacity: 0.5
}, {
duration: 800,
easing: [250, 15] // Custom bezier easing
}).then(() => {
console.log('Second animation complete!');
element.textContent = 'Done!';
});
}
});
// Example of animating another element concurrently
const anotherElement = document.createElement('div');
anotherElement.id = 'anotherElement';
anotherElement.style.cssText = `
width: 80px; height: 80px; background-color: goldenrod;
position: absolute; top: 200px; left: 50px; border-radius: 50%;
display: flex; align-items: center; justify-content: center;
color: white; font-size: 0.9em;
`;
anotherElement.textContent = 'Hello';
document.body.appendChild(anotherElement);
Velocity(anotherElement, {
left: '300px',
top: '250px',
backgroundColor: 'purple'
}, {
duration: 1000,
delay: 1000,
begin: () => console.log('Starting second element animation')
});
</script>
</body>
</html>