Jump to content
Search Community

Use a generic function to fire off different onComplete's at the same time.

dbj test
Moderator Tag

Go to solution Solved by OSUblake,

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

This title might not even make sense.

 

Here the code I'm trying to pull off:

document.addEventListener('DOMContentLoaded', function() {

  // Animate game cards into view
  function animateGameCards() {
    let elements = document.getElementsByClassName('game-card');
    console.log(elements);
    if (elements.length) {
      TweenMax.staggerFromTo(elements, 0.5, {
        autoAlpha: 0,
        y: 50
      }, {
        autoAlpha: 1,
        y: 0
      }, 0.025);
    }
  }

  // Animate tickets into view
  function animateTickets() {
    let elements = document.getElementsByClassName('ticket-wrapper');
    console.log(elements);
    if (elements.length) {
      TweenMax.staggerFromTo(elements, 0.5, {
        autoAlpha: 0,
        y: 50
      }, {
        autoAlpha: 1,
        y: 0
      }, 0.025);
    }
  }

  // Fake a quick loading state and call a tween on complete.
  function fakeLoading(fn) {
    let loading = document.getElementsByClassName('loading')[0];
    if (loading) {
      let rnd = Math.random() * (1 - 0.35) + 0.35;
      let tween = TweenMax.fromTo(loading, 0.5, {
        autoAlpha: 1
      }, {
        autoAlpha: 0,
        delay: rnd,
        onComplete: fn
      });
    } 
  }

  fakeLoading(animateGameCards);
  fakeLoading(animateTickets);

});

For this prototype I am working on I want to be able to pass a function into fakeLoading() to create a loading animation delay before the tween goes off. I want this loading tweens to happen simultaneously and have two (or more) spinners on different parts of the page spinner before the animation fires.

 

What happens when I call fakeLoading multiple times in succession is that only one of them triggers. What am I doing wrong and how can I do this right? :)

 

Thanks for your time.

Link to comment
Share on other sites

The single loading element was just to simplify things for me while trying to make this work. While making the pen for you I figured what was really going on and that I was overwriting the tween. Thanks for the help anyway :)

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