Jump to content
Search Community

Timeline not seeing new target variable

tslevack test
Moderator Tag

Go to solution Solved by GreenSock,

Recommended Posts

Having an issue getting timeline to see new variable.    I have timeline items that are targeted with variables, which works fine with original variable set on page load.  I have onClick function that sets new variable and then plays the timeline.  I even log new variable in console before play command, which shows the new variable.   The timeline plays, but loads the initial variable... not the new one that has been confirmed in console.  

 

Any thoughts why the correct variable is not being used by the timeline?   THANKS!

 

 

<script>

let activePage = ".Mdes";
let nextPage = ".Mcal";
let activeBut = ".desbut";
let nextBut = ".calbut";

let tlD2F = gsap.timeline({
  paused: true,
});

tlD2F.to(activePage, {
  zIndex: 0,
  opacity: 0,
});

tlD2F.to(nextPage, {
  zIndex: 30,
  opacity: 1,
  marginTop: "0vh",
  duration:1,
}, );

tlD2F.to(activeBut, {
  zIndex: 0,
  opacity: 0,
}, 0);

tlD2F.to(nextBut, {
  zIndex: 500,
  opacity: 1,
}, 0);


document.getElementById('D2F').onclick = function(event) {
    tlD2F.pause();
    nextPage = ".Mfea";
    nextBut = ".feabut";;
    console.log("nextPage:", nextPage); 
    console.log("nextButton:", nextBut); 
    tlD2F.play(0); 
};

</script>

Link to comment
Share on other sites

I added a function to log variable just before its called.   On click  sets new variable and logs correctly, then timeline logs correct variable just before being called.   Unfortunately, the next to() line is still loading ".Mcal" instead of ".Mfea".    How is old variable loading??? so confused. LOL

 

let activePage = ".Mdes";
let nextPage = ".Mcal";
let activeBut = ".desbut";
let nextBut = ".calbut";

let tlD2F = gsap.timeline({
  paused: true,
});

tlD2F.to(activePage, {
  zIndex: 0,
  opacity: 0,
  onComplete: function() {
    console.log("Current nextPage:", nextPage);
  }
});

tlD2F.to(nextPage, {
  zIndex: 30,
  opacity: 1,
  marginTop: "0vh",
  duration:1,
}, );

Screen Shot 2024-01-12 at 9.20.20 PM.png

Link to comment
Share on other sites

  • Solution

Oh, that's just a JavaScript/logic thing. You're misunderstanding how variables work...

let a = "a";
let copy = a;
a = "b";
console.log(copy); // "a"

JavaScript doesn't go backwards and reassign a bunch of references like that. The assignment occurs when that line executes. Period. 

 

If you're trying to animate a new target each time you click, just use a function instead, sorta like this pseudo code... 

let anim;
function transitionPage(activePage, nextPage, activeBut, nextBut) {
  if (anim) { // if there's an old animation, make sure it's killed before creating the new one...
    anim.kill();
  }
  anim = gsap.timeline();
  anim.to(...);
  // ...build your animation...
};

yourButton.addEventListener("click", () => transitionPage(...));

If you're still having trouble, please make sure you provide a minimal demo, like a CodePen, that clearly illustrates the issue so we can take a peek in context. 

 

Good luck!

  • Like 1
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...