Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

Save Automatically?

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

              
                <div id="demo"></div>
              
            
!

CSS

              
                #demo {
  opacity: 0;
  background: #ffd9d0;
  transition: opacity 1s;
}

#cycler .cycle_elements {
  opacity: 0;
  display: inline-block;
  margin: 0 20px;
}
              
            
!

JS

              
                console.clear()
var log = console.log.bind(console)

Project = function Project(el){
  
  //bunch of unrelated stuff in the constructor
  
  //set the parent element
  this.$element = el;
  
  //create a place to hold the cycling elements
  this.$cycler = $('<div id="cycler">').appendTo(this.$element);
  
  this.selectAndDraw = this.selectAndDraw.bind(this);
  this.staggered = this.staggered.bind(this);
  
  this.showParams = {opacity: 1};
  this.hideParams = {opacity: 0};
}

//hold the active timeline here
Project.prototype.timeline = null;

//so we can iterate up the alphabet
Project.prototype.testIndex = 0;

/**
 *  START PROCESSING
 */
Project.prototype.run = function () {

  //bunch of stuff happens here, preloading, unrelated code etc.

  //finished loading let's see what we have
	this.$element.css('opacity', 1);

	//now start the cycler
	console.log('starting cycler');
	this.cycler();
};

Project.prototype.cycler = function () {

  this.staggered()
  
	// let showParams = {opacity: 1},
	// 	  hideParams = {
	// opacity: 0,
	// // onComplete: this.removeLayer,
	// // onCompleteParams: ["{self}"],
	// // onCompleteScope: this,
	// };

  
  
  //repeat the timeline with 5 seconds between loops
  //should produce 'abc'-fadein-fadeout, *5s pause*, 'def'-fadein-fadeout, *5s pause*, 'ghi'-fadein-fadeout
	// this.timeline = new TimelineMax({
	// 	repeat: 2,
	// 	repeatDelay: 5,
	// });
  
  //this should populate the cycler
  //it is correctly called once in the first loop, 
  //repeated subsequent loops call it twice?!
	// this.timeline.add($.proxy(this.selectAndDraw, this));

  //this staggers the invisible elements to display
	// this.timeline.add($.proxy(staggered, this));

// 	function staggered() {

//     //populate the cycler with invisible elements
// 		// this.selectAndDraw();
   
// 		//new GSAP TimelineMax
// 		let subtimeline = new TimelineMax(),
//         elementSet = this.$cycler.children();

//     if (!elementSet.length) {
//       return;
//     }
    
// 		//attach the animations to the timeline staggered
//     //show the elements
// 		subtimeline.staggerTo(elementSet, 1, showParams, 1);
    
//     //hide the elements after 10 seconds of delay
// 		subtimeline.staggerTo(elementSet, 1, hideParams, 1, "+=10");

//     subtimeline
//       .add(this.removeLayer.bind(this, subtimeline))
//       .add(this.selectAndDraw)
//       .add(this.staggered)
    
//     //pass the timeline back to the parent
// 		return subtimeline;
// 	}

};

Project.prototype.staggered = function() {

  //populate the cycler with invisible elements
  // this.selectAndDraw();

  this.selectAndDraw()
  
  //new GSAP TimelineMax
  let subtimeline = new TimelineMax(),
      elementSet = this.$cycler.children();

  if (!elementSet.length) {
    return;
  }

  //attach the animations to the timeline staggered
  //show the elements
  subtimeline.staggerTo(elementSet, 0.2, this.showParams, 0.2);

  //hide the elements after 10 seconds of delay
  subtimeline.staggerTo(elementSet, 0.2, this.hideParams, 0.2, "+=1");

  subtimeline
    .add(this.removeLayer.bind(this, elementSet))
    .add(this.staggered)

  //pass the timeline back to the parent
  return subtimeline;
  }


Project.prototype.removeLayer = function (target) {

  $(target).remove()
  
	// console.log(tween.target);
	// //post animation, kill the layer
	// $(tween.target).remove();
}

Project.prototype.selectAndDraw = function(){
  
  if (this.testIndex > 25) {
    return;
  }
  
  console.log('select and draw called');

  //as an example, create 3 divs, each with a random letter of the alphabet as content
  let shuffledArray = [];
  
  for (let i = 0; i < 3; i++){
    shuffledArray.push(ProjectHelper.getVal(this.testIndex));
    this.testIndex++;
    
    if (this.testIndex > 25) {
      break;
    }
  } 
  
	//simplified
	$.each(shuffledArray, $.proxy(function (i, letter) {

		$('<div class="cycle_elements ' + letter + '">')
      .css({opacity: 0})
      .html('<h1>'+letter+'</h1>')
      .appendTo(this.$cycler);
	}, this));
}

let ProjectHelper = {
  
  testvals: 'abcdefghijklmnopqrstuvwxyz'.split(''),
  
  
  getVal: function(index){
    return this.testvals[index];
  },
  
  /* 
  //unused in demo
  randVal: function(){
    return this.testvals[Math.floor(Math.random() * this.testvals.length)];
  },
  shuffle: function (array) {
		for (let i = array.length - 1; i > 0; i--) {
			let j = Math.floor(Math.random() * (i + 1)),
				temp = array[i];
			array[i] = array[j];
			array[j] = temp;
		}
		return array;
	}
  */
}

let project = new Project($('#demo'));
project.run();
              
            
!
999px

Console