Jump to content
Search Community

Loading GSAP through JS then creating a timeline

Fakebook 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

I'd like to see if there's a way to load GSAP through JavaScript and then create a timeline after it's loaded and ready. I tried to use fetch + then to achieve this, but I think there's something I'm doing wrong here.

fetch('https://cdnjs.cloudflare.com/ajax/libs/gsap/1.20.3/TweenMax.min.js')
  .then(function() {
    var tl = new TimelineMax({repeat: -1});

        tl.to("#square", 1, {x: 300})
      .to("#square", 1, {x: 0})
        ;
});

 

Th error I'm receiving:

 

Uncaught (in promise) ReferenceError: TimelineMax is not defined

 

I'm not sure if this is just a JavaScript error or an error with how GSAP needs to be loaded.

See the Pen VxNKyz by anon (@anon) on CodePen

Link to comment
Share on other sites

I don't have time to look into the specifics, but I believe the problem is that you're not actually doing anything with the TweenMax file that was fetched. In other words, you've gotta put it into a <script> element and dynamically push it into the browser so that it understands it as a JavaScript resource. See what I mean? 

  • Like 1
Link to comment
Share on other sites

6 hours ago, GreenSock said:

I don't have time to look into the specifics, but I believe the problem is that you're not actually doing anything with the TweenMax file that was fetched. In other words, you've gotta put it into a <script> element and dynamically push it into the browser so that it understands it as a JavaScript resource. See what I mean? 

 

I think I know what you mean. Not sure how to do this off the top of my head but will look into it. Basically the script is loaded, but not executed?

Link to comment
Share on other sites

3 minutes ago, Fakebook said:

 

I think I know what you mean. Not sure how to do this off the top of my head but will look into it. Basically the script is loaded, but not executed?

 

Yep, that's the basic idea. It's just a long string as far as the browser is concerned. It has no idea that you want it to execute it as JavaScript until you properly inject it into the DOM. 

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