Jump to content
Search Community

Get/Set properties Greensock way.

jurito test
Moderator Tag

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

Hello there,
i'm wondering, is there a way to 'wrap' a DOM element so that i could easily get/set the current tween transforms, TweenLite-wise?
An example to be clear..
 
TweenLite.to(el1,1,{x:200,onUpdate:function(){


console.log(el1.x) //undefined
console.log(gswrapped(el1).x) //yay


}})


//or


gswrapped(el1).x=200;


//instead of the dreadful


TweenLite.to(el1,0,{x:200});
 
 
TIA for your attention!

 

Link to comment
Share on other sites

Hi,

 

Every time you tween a CSS transform property, GSAP creates an object called "_gsTransform", that stores all that information and updates it as the animation progresses. This object though acts just as a getter, the engine's native way of setting a value is the basic constructor, so for that you'll have to stick with the set method.

 

If you want to update a value from the original tween while it's running, you can use the updateTo() method, but for that you have to include TweenMax.js:

 

http://api.greensock.com/js/com/greensock/TweenMax.html#updateTo()

 

You can access the _gsTransform object during an update or at any time after the tween has been created:

// using JS selector
var el1 = document.getElementById("el1");

TweenLite.to(el1,1,{x:200,onUpdate:function(){

console.log(el1._gsTransform.x);

}});

// using jQuery
var el1 = $("#el1");

TweenLite.to(el1,1,{x:200,onUpdate:function(){

console.log(el1[0]._gsTransform.x);

}});

I´ve set up a simple codepen:

 

See the Pen FxHhm by rhernando (@rhernando) on CodePen

 

Rodrigo.

  • Like 3
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.
×
×
  • Create New...