Jump to content



.fromTo( target:Object, fromVars:Object, toVars:Object ) : Tween

[static] Static method for creating a Tween instance that allows you to define both the starting and ending values (as opposed to to() and from() tweens which are based on the target's current values at one end or the other).


target: Object

Target object (or array of objects) whose properties should be affected. When animating DOM elements, the target can be: a single element, an array of elements, a jQuery object (or similar), or a CSS selector string like “#feature” or “h2.author”. GSAP will pass selector strings to a selector engine like jQuery or Sizzle, falling back to document.querySelectorAll().

fromVars: Object

An object defining the starting value for each property that should be tweened. For example, to tween obj.xfrom 100 and obj.y from 200, fromVars would look like this: {x: 100, y: 200}.

toVars: Object

An object defining the end value for each property that should be tweened as well as any special properties likeonCompleteease, etc. For example, to tween obj.x from 0 to 100 and obj.y from 0 to 200 and then call myFunction, do this: gsap.fromTo(obj, 1, {x: 0, y: 0}, {x: 100, y: 200, onComplete: myFunction});

Returns : Tween

Returns a Tween that tweens from a given set of values to another set of values.


gsap.fromTo() is a shortcut for creating a new Tween() (although you would never explicitly create a tween by using Tween() since it is an internal class) and calling its fromTo() method.

Static method for creating a Tween instance that allows you to define both the starting and ending values (as opposed to to() and from() tweens which are based on the target’s current values at one end or the other).

NOTE: Only put starting values in the fromVars parameter - all special properties for the tween (like duration, onComplete, delay, etc.) belong in the toVars parameter.

By default, immediateRender is true in fromTo() tweens, meaning that they immediately render their starting state regardless of any delay that is specified. This is done for convenience because it is often the preferred behavior when setting things up on the screen to animate into place, but you can override this behavior by passing immediateRender: false in the fromVars or toVars parameter so that it will wait to render the starting values until the tween actually begins (often the desired behavior when inserting into a gsap.timeline instance).

Since the target parameter can also be an array of objects, the following code will tween the x property of obj1 and obj2 from 0 to 100 simultaneously:

  1. gsap.fromTo([obj1, obj2], {x: 0}, {duration: 1, x: 100});

Even though 2 objects are animating, there is still only one tween created. In order to stagger or offset the start times of each object animating, just use the stagger property:

  1. gsap.fromTo([obj1, obj2, obj3], {
  2. x: 0
  3. },
  4. {
  5. duration: 1,
  6. x: 100,
  7. stagger: 0.5 //simple stagger of 0.5 seconds
  8. });
  9. //or get advanced:
  10. gsap.fromTo([obj1, obj2, obj3], {
  11. x: 0
  12. },
  13. {
  14. duration: 1.5,
  15. x: 100,
  16. stagger: {
  17. amount: 2,
  18. from: "center",
  19. grid: "auto",
  20. onComplete: myFunction //define callbacks inside the stagger to make them apply to each sub-tween
  21. }
  22. });

For simple sequencing, you can use the delay special property (like gsap.fromTo(element, 1, {x: 0}, {x: 100, delay: 0.5})), but it is highly recommended that you consider using gsap.timeline for all but the simplest sequencing tasks. It allows you to append tweens one-after-the-other and then control the entire sequence as a whole. You can even have the tweens overlap as much as you want.

Copyright 2017, GreenSock. All rights reserved. This work is subject to theterms of useor for Club GreenSock members, the software agreement that was issued with the membership.