NexusCS

Velocity.js

JavaScript libraries
Quick reference for Velocity.js, a fast animation engine that works with and without jQuery.
wip

Getting started

Basic syntax

// With jQuery/Zepto
$element.velocity({ opacity: 0.5 }, { duration: 1000 });
// Without jQuery (utility function)
Velocity(element, { opacity: 0.5 }, { duration: 1000 });
// Comma-separated shorthand
$element.velocity({ top: 50 }, 1000, "swing");
$element.velocity({ top: 50 }, 1000, function () {
  // done callback
});

Arguments object

$element.velocity({
  properties: { opacity: 1 },
  options: { duration: 500 },
});

Installation

# NPM
npm install velocity-animate

# CDN
<script src="//cdn.jsdelivr.net/npm/velocity-animate@2.0/velocity.min.js"></script>

Animatable properties

Transform properties

Must specify axis explicitly.

$element.velocity({
  translateX: "200px",
  translateY: "100px",
  translateZ: 0, // Hardware acceleration
  rotateZ: "45deg",
  rotateX: "90deg",
  scale: 1.5,
  scaleX: 1.5,
  skewX: "10deg",
});

Color properties

$element.velocity({
  backgroundColor: "#ff0000",
  color: "#00ff00",
  colorRed: "50%",
  colorAlpha: 0.85,
});

Only hex strings supported.

SVG properties

$svgElement.velocity({
  x: 200,
  y: 100,
  cx: 50,
  cy: 50,
  r: 25,
  strokeWidth: 5,
  fill: "#ff0000",
  fillOpacity: 0.8,
});

Options

Common options

$element.velocity(
  { width: "500px" },
  {
    duration: 400, // ms or "slow"/"normal"/"fast"
    easing: "swing", // Easing function
    queue: "", // false or queue name
    delay: false, // ms
    loop: false, // integer or true
    display: undefined, // "none", "block", "auto"
    visibility: undefined,
    mobileHA: true, // Mobile hardware accel
  },
);

Duration keywords

Keyword Duration
"slow" ~600ms
"normal" ~400ms
"fast" ~200ms

Callbacks

$element.velocity(
  { opacity: 0 },
  {
    begin: function (elements) {
      // Animation starts
    },
    complete: function (elements) {
      // Animation ends
    },
    progress: function (elements, complete, remaining, start, tweenValue) {
      console.log(complete * 100 + "%");
      console.log(remaining + "ms remaining");
    },
  },
);

Easing

Named easings

// jQuery UI easings (built-in)
$element.velocity({ width: 50 }, "easeInSine");
$element.velocity({ width: 50 }, "easeOutQuad");
$element.velocity({ width: 50 }, "easeInOutCubic");
// CSS3 named easings
$element.velocity({ width: 50 }, "ease-in-out");

Custom easing

// Custom bezier curve [x1, y1, x2, y2]
$element.velocity({ width: 50 }, [0.17, 0.67, 0.83, 0.67]);
// Spring physics [tension, friction]
$element.velocity({ width: 50 }, [250, 15]);
// Step easing [steps]
$element.velocity({ width: 50 }, [8]);

Per-property easing

$element.velocity({
  borderBottomWidth: ["2px", "spring"],
  width: ["100px", [250, 15]],
  height: ["100px", "easeInSine", "0px"],
});

Value operators

Operators

$element.velocity({
  top: 50, // px assumed
  left: "50%",
  width: "+=5rem", // Add
  height: "-=10px", // Subtract
  opacity: "*=2", // Multiply
  fontSize: "/=2", // Divide
});

Supported units

Unit Description
px Pixels
em Relative to font-size
rem Relative to root
% Percentage
deg Degrees
vw Viewport width
vh Viewport height

Force-feeding

Set start value explicitly.

// Two-item array: [endValue, startValue]
$element.velocity({
  top: [500, 0],
  opacity: [0, 1],
});
// Three-item array: [endValue, easing, startValue]
$element.velocity({
  opacity: [0, "easeInSine", 1],
});

Commands

Stop

$element.velocity("stop"); // Stop current
$element.velocity("stop", true); // Stop + clear queue

Reverse

$element.velocity({ opacity: 0 });
$element.velocity("reverse");
$element.velocity("reverse", { duration: 2000 });

Finish

$element.velocity("finish"); // Jump to end values
$element.velocity("finish", true); // Finish + clear queue

Pause/Resume

$element.velocity("pause");
$element.velocity("resume");

Available in V1.4+.

Scroll

$element.velocity("scroll", {
  duration: 1000,
  offset: 250,
  axis: "y",
  container: $("#container"),
});

Fade & slide

Fade commands

$element.velocity("fadeIn", { duration: 1500 });
$element.velocity("fadeOut", { duration: 1500 });

Slide commands

$element.velocity("slideDown", { duration: 1500 });
$element.velocity("slideUp", { duration: 1500 });

Chaining & queues

Automatic queueing

$element.velocity({ width: 75 }).velocity({ height: 0 });

Custom queues

// Run in parallel (bypass queue)
$element.velocity({ height: "50px" }, { queue: false });

// Custom named queue
$element.velocity({ opacity: 0 }, { queue: "myQueue" });
$element.dequeue("myQueue");

Run sequence

$.Velocity.RunSequence([
  {
    elements: $el1,
    properties: { opacity: 1 },
    options: { duration: 1000 },
  },
  {
    elements: $el2,
    properties: { opacity: 1 },
    options: { duration: 1000 },
  },
]);

V1 only.

UI Pack

Installation

<script src="//cdn.jsdelivr.net/npm/velocity-animate@2.0/velocity.ui.min.js"></script>

Requires Velocity core.

Callout effects

$element.velocity("callout.bounce");
$element.velocity("callout.shake");
$element.velocity("callout.flash");
$element.velocity("callout.pulse");
$element.velocity("callout.tada");

Transitions

// Transition.In
$element.velocity("transition.fadeIn");
$element.velocity("transition.slideLeftIn");
$element.velocity("transition.slideUpIn");

// Transition.Out
$element.velocity("transition.fadeOut");
$element.velocity("transition.slideRightOut");

Custom effects

$.Velocity.RegisterEffect("myEffect", {
  defaultDuration: 1000,
  calls: [
    [{ opacity: 0.5 }, 0.5],
    [{ opacity: 1 }, 0.5],
  ],
});

$element.velocity("myEffect");

Advanced features

Display control

// Set display AFTER animation (fade out)
$element.velocity({ opacity: 0 }, { display: "none" });

// Set display BEFORE animation (fade in)
$element.velocity({ opacity: 1 }, { display: "block" });

// Auto-detect native display
$element.velocity({ opacity: 1 }, { display: "auto" });

Loop

$element.velocity({ height: "10em" }, { loop: 2 }); // Loop 2 times
$element.velocity({ height: "10em" }, { loop: true }); // Infinite

Promises

Velocity(element, { opacity: 0.5 })
  .then(function (elements) {
    console.log("Resolved.");
  })
  .catch(function (error) {
    console.log("Rejected.");
  });

V2 utility function only.

Gotchas

Common issues

  • Transform caching: Velocity ignores outside CSS changes to transform values. Use force-feeding for initial transforms.
  • Multi-value properties: Cannot pass { padding: "1 1 1 1" } - must use individual properties like { paddingLeft: 1, paddingRight: 1 }.
  • Color format: Only hex strings (#ff0000) supported. No RGB/HSL/keywords.
  • Loop + custom queues: Loop option and reverse do NOT work with custom queues or queue: false.
  • jQuery vs standalone: With jQuery returns jQuery object (chaining). Without jQuery must use Velocity(element, ...) for promise support.

Also see