Jump to content
Search Community

How to dynamically create elements in animation?

wong2 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, all the examples in the document assumes there are already those elements on the page, but what if I need to dynamically create elements in animation?

For example, I need to create 5 svg circles and make them move, then create 6 more circles and move them in another direction, where should I put the creation logic?

Link to comment
Share on other sites

Hi and welcome to the GreenSock forums.

 

The way I'll do it is to create a timeline, add the elements to the DOM and after add the GSAP instances to the timeline and finally when all the elements are added play the timeline.

 

Also you could make a function to create the animations and call that function after all the elements are added to the DOM.

 

This is a very simple example, but hopefully is enough to get you started:

 

See the Pen BzAYao by rhernando (@rhernando) on CodePen

  • Like 2
  • Thanks 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...