Jump to content


Tweening Parameter String Values

Recommended Posts

I'm trying to use one Tweenmax call to handle multiple tween types by using function parameters instead of declaring all the properties individually.


declaring the property as a string works fine, here i can animate 'top', 'scale', whatever

=> TweenMax.to(p_target, p_time, {'top' :  p_value} );
however passing the 'top' property as a string parameter logs invalid tween value
=> TweenMax.to(p_target, p_time, {p_type :  p_value} );
is there a way to use parameters or variables to declare which property should be animated?
thanks in advance.


Link to comment
Share on other sites

You're initializing the object with the property name "p_type". To provide a dynamic value for the name you would either need to create the object beforehand or use an ES6 computed property name. To use computed property names you will need to use a compiler like Babel, Traceur, or TypeScript since it's a new feature.

var foo = { bar: { baz: { key: "x" }}};

play("y", 200);

function play(key, value) {
  var myObject = {};
  myObject[key] = value;
  TweenLite.to("#box", 1, myObject);
  // ES6 Computed property name
  TweenLite.to("#box", 1, { [foo.bar.baz.key]: value });  

See the Pen af7229a398116492a29e198ce5d341e5 by osublake (@osublake) on CodePen

  • Like 3
Link to comment
Share on other sites

...or you could always use onUpdate to pass a tweened value to a separate function where you can then update whatever property you want.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.