Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <p class="desc">I am adding to the master timeline on button click:
  <br><br>
  • This helps me achieve my goal of animating dynamic content (real-life project this will be an ajax call).
  <br><br>
  • Click Open, then Close a couple of times and let the animations play / reverse respectively - the entire thing slows down</p>

<button class="btn-open">Open</button>
<button class="btn-close">Close</button>

<div class="box1">
  <h1>Dom-Available</h1>
</div>

<div id="load-container">


</div>


              
            
!

CSS

              
                p.desc {
  width: 500px;
  font-size:20px;
}

button{
  display:block;
  padding: 50px;
  float:left;  
}

.box1, .box2 {
  background:black;
  padding:20px;
  width:500px;
  margin:0 auto;
}

.box2 {
  background:red;
}

h1 {
  color:white;
  display: block;
}
              
            
!

JS

              
                // used Variables
var btnOpen = $('.btn-open');
var btnClose = $('.btn-close');
var addedContent = false;

// on Load - create master Timeline
var master = new TimelineMax({
	paused: true,
  onReverseComplete: function() {
    removeBox2()
  }
});


//nested animations added to master TL on Click - I think the problem is these are added every click??  
function moveDocReady() {
  var tl = new TimelineMax();
  var box1 = $('.box1');
   
	tl.to(box1, 0.5, {x: 200, ease: Power4.easeInOut}, 'sameTime');

	return tl;
}

function moveDynamic() {
  var tl = new TimelineMax();
  var box2 = $('.box2'); //reference to dynamic loaded .box2, nested in animation function so picked up when fired, therefore available for GSAP - is this the best way??  
  tl.to(box2, 0.5, {x: 200, ease: Power4.easeInOut}, 'sameTime');
  
  return tl;
}


//BtnOpen click event
btnOpen.click(function(){
  
  if(!$('.box2').length) {
    $('#load-container').append('<div class="box2"><h1>I am Dynamic</h1></div>');
      master.add(moveDocReady()) //I think it is because I add these each time btnOpen is clicked
      .add(moveDynamic());
  master.play(); 
  } 

});

//BtnClose click event
btnClose.click(function(){
  master.reverse();
});


//on reverse complete, remove box
function removeBox2() {
    if($('.box2').length) {
    $('.box2').remove();
  }
}
              
            
!
999px

Console