Jump to content


How can I make GreenSock calculate the size of an element after JavaScript has loaded?

Recommended Posts


I'm working on an animated display ad which uses dynamic content pulled in from a feed using JavaScript.


I have an unordered list that is populated with content dynamically with JavaScript, the list could contain a variable amount of list items at varying lengths. When I try an animate this list with GreenSock (revealing it by animating the height from 0px) it animates the list based on the content not being there. How can I make GreenSock see the content that gets dynamically generated with JavaScript?


Hopefully this makes sense, it's difficult to create a CodePen on this one since it links to external sources etc.



Link to comment
Share on other sites

Hi cbg,


Our beloved moderator @Jonathan taught me long ago:


// wait until DOM is ready
document.addEventListener("DOMContentLoaded", function(event) {
  window.addEventListener("load", function(){
      // place your code here


That will cause all the code nested inside the event listeners to wait until the DOM is ready and all assets are loaded. You should then be able to tween your elements as you desire.

  • Like 3
Link to comment
Share on other sites

Thank you!


Do you know how I can make the following run, only if the image exists in the DOM?

.to ("#image2", 0.5, {opacity:0, delay: 3})


Link to comment
Share on other sites

Would it be a conditional? Sometimes being there, sometimes not?


If so, wrap the line into a if() statement.


// Somewhere at the start

const img2 = document.querySelector("#image2")

// Timeline setup

const tl = new TimelineMax()

tl.to( "image1", 1, { x: 100 } )

if( img2 ) {
  tl.to( img2, 1, { autoAlpha: 0 } )


tl.to( "image3", 1, { autoAlpha: 0 } )


Also, better than adding a delay parameter to a tween in a timeline is to use the position parameter:


// not wrong
.to ("#image2", 0.5, {opacity:0, delay: 3})

// better
.to ("#image2", 0.5, {opacity:0}, "+=3")


  • Like 5
Link to comment
Share on other sites

  • 3 months later...

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.