Jump to content
GreenSock

Timeline

.to()

.to( target:Object, vars:Object, position:* ) : *

Adds a gsap.to() tween to the end of the timeline (or elsewhere using the position parameter) - this is a convenience method that accomplishes exactly the same thing as add( gsap.to(...) ) but with less code.

Parameters

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().

vars: Object

An object defining the end value for each property that should be tweened as well as any special properties like onCompleteease, etc. For example, to tween element’s left to 100 and element’s top to 200 and then call myFunction, do this: gsap.timeline().to(element, {duration: 1, left: 100, top: 200, onComplete: myFunction}).

position: *

(default = +=0) — Controls the placement of the tween in the timeline (by default, it’s the end of the timeline, like “+=0”). Use a number to indicate an absolute time in terms of seconds, or you can use a string with a “+=” or “-=” prefix to offset the insertion point relative to the END of the timeline. For example, "+=2" would place the tween 2 seconds after the end, leaving a 2-second gap. "-=2" would create a 2-second overlap. You may also use a label like "myLabel" to have the tween inserted exactly at the label or combine a label and a relative offset like "myLabel+=2" to insert the tween 2 seconds after “myLabel” or "myLabel-=3" to insert it 3 seconds before “myLabel”. If you define a label that doesn’t exist yet, it will automatically be added to the end of the timeline before inserting the tween there which can be quite convenient. Be sure to read our tutorial Understanding the Position Parameter which includes interactive timeline visualizations and a video.

Returns : *

self (makes chaining easier)

Details

Adds a Tween.to() tween to the end of the timeline (or elsewhere using the position parameter) - this is a convenience method that accomplishes exactly the same thing as add( gsap.to(...) ) but with less code. In other words, the following two lines produce identical results:

  1. myTimeline.add( gsap.to(element, {duration: 1, left: 100, opacity: 0.5}) );
  2. myTimeline.to(element, {duration: 1, left: 100, opacity: 0.5});

Keep in mind that you can chain these calls together and use other convenience methods like fromTo(), call(), set(), etc. to build out sequences very quickly:

  1. //create a timeline that calls myFunction() when it completes
  2. var tl = gsap.timeline({onComplete: myFunction});
  3. //now we'll use chaining, but break each step onto a different line for readability...
  4. tl.from(element, {duration: 1, left: -100}) //tween element's left from -100
  5. //then tween element's "top" to 50
  6. .to(element, {duration: 1, top: 50})
  7. //then set element's opacity to 0.5 immediately
  8. .set(element, {opacity: 0})
  9. //then call otherFunction()
  10. .call(otherFunction)
  11. //finally tween the rotation of element1, element2, and element3 to 45 and stagger the start times by 0.25 seconds
  12. .to([element1, element2, element3], {duration: 1.5, rotation:45, stagger: 0.25});

If you don’t want to append the tween and would rather have precise control of the insertion point, you can use the additional position parameter. Or use a regular add() like tl.add( gsap.to(element, {duration: 1, left: 100}), 2.75).

The 3rd parameter is the position which controls the placement of the tween in the timeline (by default, it’s at the end of the timeline). Use a number to indicate an absolute time in terms of seconds, or you can use a string with a “+=” or “-=” prefix to offset the insertion point relative to the END of the timeline.

For example, "+=2" would place the tween 2 seconds after the end, leaving a 2-second gap. "-=2" would create a 2-second overlap. You may also use a label like "myLabel" to have the tween inserted exactly at the label or combine a label and a relative offset like "myLabel+=2" to insert the tween 2 seconds after “myLabel” or "myLabel-=3" to insert it 3 seconds before “myLabel”. If you define a label that doesn’t exist yet, it will automatically be added to the end of the timeline before inserting the tween which can be quite convenient.

  1. tl.to(element, {duration: 1, left: 100}); //appends to the end of the timeline
  2. tl.to(element, {duration: 1, left:100}, 2); //appends it at exactly 2 seconds into the timeline (absolute position)
  3. tl.to(element, {duration: 1, left:100}, "+=2"); //appends it 2 seconds after the end (with a gap of 2 seconds)
  4. tl.to(element, {duration: 1, left:100}, "myLabel"); //places it at "myLabel" (and if "myLabel" doesn't exist yet, it's added to the end and then the tween is inserted there)
  5. tl.to(element, {duration: 1, left:100}, "myLabel+=2"); //places it 2 seconds after "myLabel"
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.
×