Search code examples
javascriptgsap

TweenLite Variable with #ID


I can't understand how it works. Im trying to add a variable in my TweenLite scrollto plugin.

My Variable name is section:

var section = 2;

document.getElementById("corner-bottom").onclick = function() {
  if(section < 4){
    section++;
  }
  TweenLite.to(window, 1, {scrollTo:{"#section" + section, offsetY:70}});
}

Im getting "Unexpected token +" error.

How can I add variable into my scrollTo id?


Solution

  • Your syntax is wrong in your posted code, due to you missing the y parameter with your id and concatenated variable.

    Try this:

    TweenMax.to(window, 1, {scrollTo:{y:"#section" + section, offsetY:70}});
    

    Please see the GSAP ScrollToPlugin Docs for more info:

    https://greensock.com/docs/Plugins/ScrollToPlugin

    :)