Velvet
A JavaScript library for DOM animations as smooth as Velvet, which was inspired by Velocity and jQuery Transit.
TL;DR
See demo page.
Features
-
Animations with JavaScript
It's a sort of manipulator to easily manipulate an element's transform property with JavaScript. You can freely play, pause and reverse the animation. -
Web Animations API
It preferentially uses Web Animations API, specificallyelement.animate()
if supported.
Policy
-
Speedster
It pursues speed than Swiss Army knife. -
Minimalist
It keeps it as simple and small as possible (3.3kb (minified and gzipped) for now).
Installation
npm:
$ npm install velvetjs
Bower(Deprecated):
$ bower install velvetjs
Usage
CommonJS:
var velvet = ;var div = ;div;
AMD:
;
directly:
API
velvet([element])
- element: One or more target elements. (optional)
The velvet
function returns a Velvet
object or a Textile
object. If call this with a parameter, it returns a Velvet
object. If omit the parameter, it returns a Textile
object.
var v =
Velvet object
v.weave(styles, options)
- styles: CSS Styles (transforms and opacity only)
- options: Animation options
The weave
method creates an animation. The details of arguments are as follows:
var w = v;
NOTE: Don't append unit for these parameters. 100px -> 100
This method returns a Weaver
object and the animation will automatically start.
v.style(styles)
- styles: CSS Styles
The style
method set styles to the element. Since the above weave
method ignores the preset transform
and opacity
property, you need to set the styles if want to make started the animation from status different from default (position, size, etc.).
// fade-invstyle translateX: 100 opacity: 0;
You may also set other properties with this method.
vstyle position: 'absolute' left: '10px' // In this case you need to append unit. 10 -> 10px top: '20px';
NOTE: Since this method will not take care of Vendor Prefix, for instance, you will need to set them like this:
vstyle '-webkit-transform-origin': 'left top' '-moz-transform-origin': 'left top' '-ms-transform-origin': 'left top' 'transform-origin': 'left top';
Textile object
t.weave(from, to, options, cb)
- from: An array of start values
- to: An array of end values
- options: Animation options
- cb: A callback function called per frame
The weave
method creates an animation other than transform
and opacity
. This is an example of scrolling a window:
var w = t;
It returns a Weaver
object in the same way as v.weave
method.
Weaver object
w.pause()
The pause
method pauses the animation.
w.play()
The play
method resumes the paused animation, or replays the stopped animation.
w.finish()
The finish
method goes to the end position of the animation, and stops.
w.cancel()
The cancel
method goes back to the start position of the animation, and stops.
w.reverse()
The reverse
method reverses the current play direction, and starts to play the animation.
v;
w.direction()
The direction
method returns current play direction. (1: forward direction, -1: backward direction)
Tips
Position Property
For better performance, set absolute
or fixed
to the element's position
property.
#element { position: "absolute";}
Combo Animations with Promise
In order to create Combo Animations use Promise like this:
var div =
TODO
- Proper test
Multiple elements control- Customizing cubic bezier
- Vendor Prefix support for
style
method - Returning a Promise
Supported browser
Chrome, Firefox, Safari, Opera, Android Browser 4.0+, iOS Safari, Edge and IE9+
Lisence
MIT