Jump to content

Leeroy J

Adjust animation time length depending on text character length?

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 have a simple timeline that shows my headline then fades out. 


<div id="headline1">Dynamic text that may be 1 - 60 characters.</div>


  var tl = new TimelineMax();
      tl.from("#headline1", 0.4, {autoAlpha:0}) 		//headline fade in
        .to("#headline1", 0.4, {autoAlpha:0}, "+=3.5"); 	//headline fade out


Is it possible to make my headline visible depending on how many characters are in my headline?



10 characters = 1 second before fading out

60 characters = 5 seconds before fading out

Link to comment
Share on other sites

Sure can ... just grab the string length and perform some operation on it to get your desired delay. Then assign that delay value to the delay property. Here's an example,


See the Pen KmJmXy?editors=1010 by sgorneau (@sgorneau) on CodePen


  • Like 5
Link to comment
Share on other sites

Thanks @Shaun Gorneau your code was just what I was after.


This is what I ended up using (plain javascript)



  delay = Math.ceil(dynamic_headline1.length/14);
  console.log("headline1 is visable for " + dynamic_headline1.length/14 + " seconds");
  var tl = new TimelineMax();
        .to("#headline1", 0.8, {autoAlpha:0, delay: delay})  //text fade out
    return tl;
  • 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.