.animate()
Syntax Variants
Parameters
- properties
- a property map describing the end values of the corresponding properties. The names can use the
set() syntax ('@' prefix for attributes, '$' for styles, '$$fade' for fading, '$$slide' for slide effects,
'$$scrollX' and '$$scrollY' for scroll coordinates).
Values must be either numbers, numbers with units (e.g. "2 px") or colors ('rgb(r,g,b)', '#rrggbb' or '#rgb') or
a
function(oldValue, index, obj)to determine the new value. The function will be invoked for each list element to evaluate the new value:- oldValue
- The old value of the property to be changed, as returned by
get(). For the CSS style names, this is the computed style of the property - index
- The list index of the object owning the property
- obj
- The list element owning the property.
- (callback return value)
- The destination value to be set.
function(startValue, endValue, t) which will be
called every time an interpolated value is required:
- startValue
- The start value of the transition.
- endValue
- The end value of the transition.
- t
- A value between 0 and 1 that specifies the state of the transition.
- (callback return value)
- The value at the time t.
function(list):
- list
- A reference to the animated list.
function() without arguments.
The returned promise also has property 'stop', which is a function. Invoke the function without arguments to
interrupt a running animation. It will return the animations actual duration in ms.
Description
Animates the items of the list by modifying their properties, CSS styles and attributes. animate() can work with numbers, strings that contain exactly one number, and with colors in the CSS notations 'rgb(r,g,b)', '#rrggbb' or '#rgb'.
When you invoke the function, it will first read all old values from the object and extract their numbers and colors. These start values will be compared to
the destination values that have been specified in the given properties. Then animate() will create a background task using $.loop() that updates the
specified properties in frequent intervals so that they transition to their destination values.
The start values will be obtained using get(). It is recommended to set the start values using set() before you start the animation, even if this is not
always required.
You can define the kind of transition using the linearity parameter. If you omit it or pass 0, animate's default algorithm will cause a smooth transition from the start value to the end value. If you pass 1, the transition will be linear, with a sudden start and end of the animation. Any value between 0 and 1 is also allowed and will give you a transition that is 'somewhat smooth'.
Instead of the linearity function you can provide your own interpolation function(startValue, endValue, t) which will be
called every time an interpolated value is required. startValue and endValue define the start and end values. t
is a value between 0 and 1 that specifies the current state of the transition. The function must return the startValue for 0 and
the endValue for 1. For values between 0 and 1, the function should return a transitional value.
If the start value of a property is a string containing a number, animate() will always ignore all the surrounding text and use the destination value as a template for the value to write. This can cause problems if you mix units in CSS. For example, if the start value is '10%' and you specify an end value of '20px', animate will do an animation from '10px' to '20px'. It is not able to convert units.
animate() does not only support strings with units, but any string containing exactly one number. This allows you, among other things, to work with IE-specific CSS properties. For example, you can transition from a start value 'alpha(opacity = 0)' to 'alpha(opacity = 100)'.
When you animate colors, animate() is able to convert between the three notations rgb(r,g,b), #rrggbb or #rgb. You can use them interchangeably, but you can not use color names such as 'red'.
Instead of the end value, you can also specify a function(oldValue, index, obj) to calculate the actual end value.
To allow more complex animation, animate() returns a Promise that is fulfilled when the animation has finished. You can also stop
a running animation by calling the promise's stop() function. If you only use the Web module, stop() is only available in the promise returned by
animate(). If you have the full package, the stop function will be propagated and can be called at any point of a promise chain.
Example
Move an element:
$('#myMovingDiv').set({$left: '0px', $top: '0px'}) // start values
.animate({$left: '50px', $top: '100px'}, 1000); // animation
Example
Change the color of an element:
$('#myBlushingDiv').set({$backgroundColor: '#000000'})
.animate({$backgroundColor: '#ff0000'}, 1000);
Example
Using a function to calulate the values for animation:
$('#myMovingDiv').animate({$left: function(v) { return (parseFloat(v) + 100) + 'px'; }}, 1000);
Example
Fade-out effect:
$('#myFadingDiv').animate({$$fade: 0}, 1000);
Example
Slide-in effect:
$('#myInvisibleDiv').animate({$$show:1, $$slide: 1}, 1000);
Example
Stopping a simple animation. This requires only the Web module.
var div = $('#myMovingDiv').set({$left: '0px', $top: '0px'});
var p = div.animate({$left: '800px', $top: '0px'}, 5000, 0);
$('#stopButton').on('click', p.stop);
});
Example
Chained animation using Promise callbacks. The element is first moved to the position 200/0, then to 200/200
and finally moves to 100/100.
A stop button allows you to interrupt the animation.
Please note that while chaining animations requires only the Web module,
stopping a chained animation requires the full distribution with both Web and Util module. Only the complete Promises implementation
supports this.
var div = $('#myMovingDiv').set({$left: '0px', $top: '0px'});
var p = div.animate({$left: '200px', $top: '0px'}, 600, 0)
.then(function() {
return div.animate({$left: '200px', $top: '200px'}, 800, 0);
}).then(function() {
return div.animate({$left: '100px', $top: '100px'}, 400);
});
$('#stopButton').on('click', p.stop); // stopping requires Web+Util modules!
});
See also..
toggle()can be used to define animations between two states.$.loop()allows you to write more complex animations.
Comments
comments powered by DisqusFunctions
- $() Web
- list.length Web, Util
- $$() Web
- $.getCookie()
- $.loop() Web
- $.off() Web
- $.parseJSON() Web
- $.ready() Web
- $.request() Web
- $.setCookie()
- $.toJSON() Web
- $.wait()
- .add() Web
- .addAfter() Web
- .addBefore() Web
- .addFront() Web
- .animate() Web
- .array() Util
- .call() Util
- .clone() Web
- .collect() Web, Util
- .contains() Util
- .dial() Web
- .each() Web, Util
- .endsWith() Util
- .equals() Util
- .fill() Web
- .filter() Web, Util
- .find() Web, Util
- .findLast() Web, Util
- .get() Web
- .hide() Web
- .ht()
- .intersection() Util
- .is() Web
- .join() Util
- .map() Util
- .merge() Util
- .next() Web
- .not() Web
- .offset()
- .on() Web
- .onChange() Web
- .onClick() Web
- .onFocus() Web
- .onOver() Web
- .only()
- .per() Util
- .reduce() Util
- .remove() Web
- .replace() Web
- .reverse() Util
- .select() Web
- .set() Web
- .show() Web
- .sort() Util
- .startsWith() Util
- .sub() Web, Util
- .text() Web
- .toObject() Util
- .toggle() Web
- .trav() Web
- .trigger() Web
- .uniq() Util
- .unite() Util
- .up() Web
- .values() Web
- EE() Web
- HTML() Web
- M Web, Util
- MINI.getter Web
- MINI.setter Web
- Minified Lists Web, Util
- Promise Web, Util
- _() Util
- _.bind() Util
- _.copyObj() Util
- _.dateAdd() Util
- _.dateClone() Util
- _.dateDiff() Util
- _.dateMidnight() Util
- _.eachObj() Util
- _.escapeHtml() Util
- _.escapeRegExp() Util
- _.extend() Util
- _.filterObj() Util
- _.format() Util
- _.formatHtml() Util
- _.formatValue() Util
- _.isBool() Util
- _.isDate() Util
- _.isEmpty() Util
- _.isFunction() Util
- _.isList() Util
- _.isNumber() Util
- _.isObject() Util
- _.isString() Util
- _.isValue() Util
- _.keys() Util
- _.mapObj() Util
- _.pad() Util
- _.parseDate() Util
- _.parseNumber() Util
- _.partial() Util
- _.promise()
- _.range()
- _.template() Util
- _.toString() Util
- _.trim() Util
- _.values() Util
- define() Web, Util
- promise.always()
- promise.error() Web, Util
- promise.fire()
- promise.stop()
- promise.then() Web
- require() Web, Util
- How to...