Jump to content
Search Community

Increment a value with a variable

Ismail Cherri 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

Hi there,

First, I would like to thank you for your efforts and your amazing tweening engine :)

Secondly, I was wondering how to do the following:

var myIncreasingNumber = 5;
TweenMax.to(element, 1, {left:+=myIncreasingNumber}); 

I want something similar to:

TweenMax.to(element, 1, {left:"+=5"});

 

Many thanks in advance...

Link to comment
Share on other sites

This one is super simple and will make your life very easy:

TweenMax.to(element, 1, {left:"+="+myIncreasingNumber});

+ is also the string concatenator in Javascript (with automatic evaluation), so myIncreasingNumber gets converted to a string and concatenated to the "+=" e.g.

myIncreasingNumber = 5;
"+="+myIncreasingNumber === "+="+"5" === "+=5"

One thing to watch out for when concatenating strings in javascript:

"somestring="+5+7   === "somestring=57"
"somestring="+(5+7) === "somestring=12"
  • 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.
×
×
  • Create New...