Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
oby1

How to stagger?

Go to solution Solved by oby1,

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

Hello, 

 

 

so I am trying to animate letters individualy. for this purpose  I'm taking text out of a jason object, breaks it into individual letters, and set each individual letter in its own <div> . the ideas is to be able to animate each letter individualy using stagger. 
 
However, as you can see TweenMax.staggerTo(elements, 5.5, { opacity:1});  changes the opacity of all the elements at the same time.
 

Any idea why stagger is not working as I think it should?

<div id = 'test'></div>
#test { 
        height: 500px;
	width: 500px;
	position:absolute;			    
	}
			
.textlineholder {
	width: 100%;
	display:inline-block;
	position: relative;
	}
			 
.letter {
	display:inline;
	position: relative;
	opacity: 0;
        }
$( document ).ready(function() {
   
	var test = document.getElementById('test');
	
		var myArray = {
                             "text1": [
                          	
                                {
                                   "txt":"Hello, hello, hello..",
                                   "x": "0",
                                   "y": "50",
                                   
                                },
                          	
                                {
                                   "txt":"is there anybody in there?",
                                   "x": "0",
                                   "y": "100",
                                   
                                },
                          	  
                          	  {
                                   "txt":"just nod if you can hear me",
                                   "x": "0",
                                   "y": "150",
                                   
                                },
                             ]
                        } ;
										
	for ( var i=0 ; i < myArray.text1.length ; i++) {
	
	      var linecontainer = document.createElement('div'),
		  letters = myArray.text1[i].txt.split("");
			  
	      linecontainer.id = 'line'+i
	      test.appendChild(linecontainer);
	      linecontainer.className = 'textlineholder';
	         	  
    	      for ( var k=0 ; k < letters.length ; k++){
                    textdiv = document.createElement('div');	  
        	    textdiv.id = 'textdiv'+i+k;
    		    linecontainer.appendChild(textdiv);
    		    textdiv.innerHTML = letters[k];
    		    textdiv.className = 'letter';	       
    		  }  
	}	
    	  
    TweenMax.staggerTo($('.letter'), 5.5, { opacity:1});  					  
});

See the Pen qOYZmy by ohavben (@ohavben) on CodePen

Link to comment
Share on other sites

oh silly me, i forgot to designate a delay to the stagger function...

 

so 

TweenMax.staggerTo($('.letter'), 0.5, { opacity:1},0.5 ); 

adding the 0.5 delay fixed it

  • Like 2
Link to comment
Share on other sites

  • Solution

Actually changed it to a time line for better control and continouse animation.

var elements = document.getElementsByClassName('letter'),
			
	 tl = new TimelineMax({repeat:-1, repeatDelay:2});
		 tl.add( TweenMax.set(elements, {opacity:0}));
		 tl.add( TweenMax.staggerTo(elements, 0.25, { opacity: 1}, 0.25));		        
                 tl.add( TweenMax.to(elements, 3, {opacity:0}));

         elements.animation = tl;	        
Link to comment
Share on other sites

I don't know if you know about the SplitText gsap plugin. But I assume it does all the heavy lifting for what you are trying to achieve?

 

Here's a SplitText demo: 

See the Pen gFHza by GreenSock (@GreenSock) on CodePen


 

If you aren't a member of Club GreenSock, but still want to take the SplitText plugin for a drive, you can use the CodePen specific demo plugins here:

See the Pen OPqpRJ by GreenSock (@GreenSock) on CodePen


 

Link to comment
Share on other sites

Hello oby1

 

The add() method is really best when used with nested timelines. But since your just using one timeline than You should change your code to the below:

var elements = document.getElementsByClassName('letter'),
			
var tl = new TimelineMax({repeat:-1, repeatDelay:2}; // always make sure to declare your variable with 'var'

tl.set(elements, {opacity:0}));
tl.staggerTo(elements, 0.25, { opacity: 1}, 0.25);		        
tl.to(elements, 3, {opacity:0});

elements.animation = tl;	

Keep in mind that when you use the add() method, the default is to add the tween at the end of the timeline. Also you should always as best practice declare your variables with var so you don't have issues when the browsers JavaScript parses your code.

 

Also instead of using opacity  you can use autoAlpha.

 

autoAlpha is part of the GSAP CSSPlugin and will help with performance of your animation.

  • Identical to opacity except that when the value hits 0 the visibility property will be set to "hidden" in order to improve browser rendering performance and prevent clicks/interactivity on the target. When the value is anything other than 0, visibility will be set to "inherit". It is not set to "visible" in order to honor inheritance (imagine the parent element is hidden - setting the child to visible explicitly would cause it to appear when that's probably not what was intended). And for convenience, if the element's visibility is initially set to "hidden" and opacity is 1, it will assume opacity should also start at 0. This makes it simple to start things out on your page as invisible (set your css visibility:hidden) and then fade them in whenever you want.
//fade out and set visibility:hidden
TweenLite.to(element, 2, {autoAlpha:0});
 
//in 2 seconds, fade back in with visibility:visible
TweenLite.to(element, 2, {autoAlpha:1, delay:2});

Hope this helps! :)

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