Merge pull request #2789 from SergioCrisostomo/upgrade-karma-sauce-launcher
[mootools.git] / Docs / Fx / Fx.Tween.md
blob115c931a6bc6a8bcce2e9d7394abbd227a6d115d
1 Class: Fx.Tween {#Fx-Tween}
2 ===========================
4 Contains [Fx.Tween][] and the Element shortcut [Element.tween][].
6 ### Extends:
8 [Fx][]
12 Fx.Tween Method: constructor {#Fx-Tween:constructor}
13 ----------------------------------------------------
15 The Tween effect, used to transition any CSS property from one value to another.
17 ### Syntax:
19         var myFx = new Fx.Tween(element, [, options]);
21 ### Arguments:
23 1. element  - (*mixed*) An Element or the string id of an Element to apply the transition to.
24 2. options  - (*object*, optional) The [Fx][] options object, plus the options described below:
26 ### Options:
28 * property - (*string*) The CSS property to transition to, for example 'width', 'color', 'font-size', 'border', etc. If this option is omitted, you are required to use the property as a first argument for the start and set methods. Defaults to null.
31 ### Notes:
33 - Any CSS property that can be set with Element:setStyle can be transitioned with Fx.Tween.
34 - If a property is not mathematically calculable, like border-style or background-image, it will be set immediately upon start of the transition.
35 - If you use the property option, you must not use the property argument in the start and set methods.
37 ### See Also:
39 - [Fx][]
41 ### Examples:
43 Tweens the height of a element while clicking a link (which stops the default behavior), using a transition and a long duration.
44 It uses the `link` option so when clicking the link twice it behaves smoothly. When the start value is omitted, the current
45 value of the property (in this example the height property) will be used.
47         var myFx = new Fx.Tween('myElement', {
48                 duration: 'long',
49                 transition: 'bounce:out',
50                 link: 'cancel',
51                 property: 'height'
52         });
54         document.id('myLink').addEvent('click', function(event){
55                 event.stop();
56                 myFx.start(40, 100);
57         });
59 It is also possible to use the Element properties: `.get('tween')` and `.set('tween')` and the `tween` method.
60 In this example the property method is not set as an option, now it should be set as argument of the `tween` method.
61 This is something you can choose for both the `Fx.Tween` constructor or this approach.
63         var myElement = document.id('myElement');
64         myElement.set('tween', {
65                 duration: 'long',
66                 transition: 'bounce:out',
67                 link: 'cancel'
68         });
70         document.id('myLink').addEvent('click', function(event){
71                 event.stop();
72                 myElement.tween('height', 40, 100);
73         });
77 Fx.Tween Method: set {#Fx-Tween:set}
78 ------------------------------------
80 Sets the Element's CSS property to the specified value immediately.
82 ### Syntax:
84         myFx.set(property, value);
86 ### Arguments:
88 1. property - (*string*) The css property to set the value to. Omit this if you use the property option.
89 2. value - (*mixed*) The value to set the CSS property of this instance to.
91 ### Returns:
93 * (*object*) This Fx.Tween instance.
95 ### Examples:
97         var myFx = new Fx.Tween(element);
98         // sets the background color of the element to red:
99         myFx.set('background-color', '#f00');
101 ### Note:
103 If you use the property option, you must not use the property argument in the start and set methods.
106 Fx.Tween Method: start {#Fx-Tween:start}
107 ----------------------------------------
109 Transitions the Element's CSS property to the specified value.
111 ### Syntax:
113         myFx.start([property,] [from,] to);
115 ### Arguments:
117 1. property - (*string*, if not in options) The css property to tween. Omit this if you use the property option.
118 2. from     - (*mixed*, optional) The starting CSS property value for the effect.
119 3. to       - (*mixed*) The target CSS property value for the effect.
121 ### Returns:
123 * (*object*) This Fx.Tween instance.
125 ### Examples:
127         var myFx = new Fx.Tween(element);
128         // transitions the background color of the Element from black to red:
129         myFx.start('background-color', '#000', '#f00');
130         // transitions the background color of the Element from its current color to blue:
131         myFx.start('background-color', '#00f');
133 ### Notes:
135 - If only one argument is provided, other than the property argument, the first argument to start will be used as the target value, and the initial value will be calculated from the current state of the element.
136 - When using colors, either RGB or Hex values may be used.
137 - If you use the property option, you must not use the property argument in the start and set methods.
141 Object: Element.Properties {#Element-Properties}
142 ==============================================
144 see [Element.Properties][]
146 Element Property: tween {#Element-Properties:tween}
147 ---------------------------------------------------
149 Sets and gets default options for the Fx.Tween instance of an Element.
151 ### Setter:
153 #### Syntax:
155         el.set('tween'[, options]);
157 #### Arguments:
159 * options - (*object*) the Fx.Tween options.
161 #### Returns:
163 * (*element*) This Element.
165 #### Examples:
167         el.set('tween', {duration: 'long'});
168         el.tween('color', '#f00');
170 ### Getter:
172 #### Syntax:
174         el.get('tween');
176 #### Arguments:
178 1. property - (*string*) the Fx.Tween property argument.
180 #### Returns:
182 * (*object*) The Element's internal Fx.Tween instance.
184 #### Examples:
186         el.get('tween').start(0);
188 ### Notes:
190 - When initializing the Element's tween instance with Element:set, the property to tween SHOULD NOT be passed.
191 - The property must be specified when using Element:get to retrieve the actual Fx.Tween instance, and in calls to Element:tween.
192 - When options are passed to the setter, the instance will be reset.
193 - As with the other Element shortcuts, the difference between a setter and a getter is that the getter returns the instance, while the setter returns the element (for chaining and initialization).
197 Type: Element {#Element}
198 ========================
200 Custom Type to allow all of its methods to be used with any DOM element via the dollar function [$][].
204 Element Method: tween {#Element:tween}
205 --------------------------------------
207 Element shortcut method which immediately transitions any single CSS property of an Element from one value to another.
209 ### Syntax:
211         myElement.tween(property, startValue[, endValue]);
213 ### Arguments:
215 1. property   - (*string*) the css property you want to animate. Omit this if you previously set the property option.
216 2. startValue - (*mixed*) The start value for the transition.
217 2. endValue   - (*mixed*) The end value for the transition. If this is omitted, startValue will be used as endValue.
219 ### Returns:
221 * (*element*) This Element.
223 ### Examples:
225         // transitions the width of 'myElement' from its current width to 100px:
226         $('myElement').tween('width', '100');
227         // transitions the height of 'myElement' from 20px to 200px:
228         $('myElement').tween('height', [20, 200]);
229         // transitions the border of 'myElement' from its current to '6px solid blue':
230         $('myElement').tween('border', '6px solid #36f');
232 ### See Also:
234 - [Fx.Tween][]
238 Element Method: fade {#Element:fade}
239 ------------------------------------
241 Element shortcut method for tween with opacity.  Useful for fading an Element in and out or to a certain opacity level.
243 ### Syntax:
245         myElement.fade([how]);
247 ### Arguments:
249 1. how - (*mixed*, optional: defaults to 'toggle') The opacity level as a number or string representation.  Possible values include:
250  * 'in'     - Fade the element to 100% opacity.
251  * 'out'    - Fade the element to 0% opacity.
252  * 'show'   - Immediately set the element's opacity to 100%.
253  * 'hide'   - Immediately set the element's opacity to 0%.
254  * 'toggle' - If visible, fade the element out, otherwise, fade it in.
255  * (*number*)  - A float value between 0 and 1. Will fade the element to this opacity.
257 ### Returns:
259 * This Element.
261 ### Examples:
263         $('myElement').fade('out'); // fades 'myElement' out.
264         $('myElement').fade(0.7); // fades 'myElement' to 70% opacity.
268 Element Method: highlight {#Element:highlight}
269 ----------------------------------------------
271 Element shortcut method for tweening the background color.  Immediately transitions an Element's background color to a specified highlight color then back to its set background color.
273 ### Syntax:
275         myElement.highlight([start, end]);
277 ### Arguments:
279 1. start - (*string*, optional: defaults to '#ff8') The color from which to start the transition.
280 2. end - (*string*, optional: defaults to Element's set background-color) The background color to return to after the highlight effect.
282 ### Note:
284 If no background color is set on the Element, or its background color is set to 'transparent', the default end value will be white.
286 ### Returns:
288 * (*element*) This Element.
290 ### Examples:
292     //immediately changes the background to light blue, then back to its original color (or white):
293         $('myElement').highlight('#ddf');
295         //immediately changes the background to light blue, then fades to grey:
296         $('myElement').highlight('#ddf', '#ccc');
299 [$]: /core/Element/Element#Window:dollar
300 [Fx]: /core/Fx/Fx
301 [Fx.Tween]: #Fx-Tween
302 [Element.tween]: #Element-Properties:tween
303 [Element.Properties]: /core/Element/Element/#Element-Properties