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

              
                
<div class="graduation_twinklers">
  <div id="twinkler_1" class="twinkler" aria-hidden="true"></div>
  <a class="graduation_twinklers_cta" href="https://www.york.ac.uk/students/studying/graduation/">
    <h1>Congratulations to today's graduates</h1>
    <p><strong>Watch ceremonies online and find out all you need to know for your big day</strong></p>
  </a>
	<div id="twinkler_2" class="twinkler" aria-hidden="true"></div>
</div>
              
            
!

CSS

              
                /* -------------------------------------------------- */

* { box-sizing: border-box; }

body
{
  margin: 0;
  background: #f0f0f0;
  font-family: chaparral-pro,"Times New Roman",Times,serif;
}

h1
{
  font-family: chaparral-pro,"Times New Roman",Times,serif;
  font-weight: 600;
}

/* -------------------------------------------------- */

.graduation_twinklers
{
  position: relative;
  
	background: #DA1A33 url( 'twinkler_bg_mobile.png' ) center / contain no-repeat;
	color: white;

	overflow: hidden;
	padding: 2rem calc( 50% - 40em );

	display: block;
	text-decoration: none;
	
	font-family: chaparral-pro,"Times New Roman",Times,serif;
	font-weight: normal;
}

@media screen and ( min-width:600px )
{
	.graduation_twinklers
	{
		background: #DA1A33 url( 'twinkler_bg.png' ) center right / contain no-repeat;
	}
}

.graduation_twinklers_cta
{
	display: block;
	margin: 1.5rem 1rem;

	text-align: center;

	color: inherit;
	text-decoration: none;
}

.graduation_twinklers_cta:hover
{
	color: inherit;
	text-decoration: none;
}

.graduation_twinklers_cta:hover p
{
	text-decoration: underline;
}

.graduation_twinklers_cta h1
{
	text-transform: uppercase;
	margin: 0;
}

.graduation_twinklers_cta p
{
	margin: 0;
}

/* Layers */

.twinkler_layer
{
	padding: 0 0.25rem;

	display: flex;
	flex-flow: row wrap;
	justify-content: space-around;
}

@media screen and ( min-width:700px ) { .twinkler_layer { padding: 0 1rem; } }

/* Stack all layers on top of the first */

.twinkler { position: relative; }

.twinkler_layer:not( :first-child )
{
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
}

/* The twinkling items */

.twinkler_layer > *
{
	height: 1.5em;
	line-height: 1.5em;
	
	flex-grow: 0;
	flex-shrink: 0;

	text-align: center;
	white-space: nowrap;
}

/* Font sizing */

.twinkler_layer > * { font-size: 0.7em; }

@media screen and ( min-width:400px ){ .twinkler_layer > * { font-size: 0.8em; } }
@media screen and ( min-width:600px ){ .twinkler_layer > * { font-size: 0.9em; } }
@media screen and ( min-width:900px ){ .twinkler_layer > * { font-size: 1em; } }

/* Transitions */

/* Default/dormant */
.twinkler_layer > *
{
	pointer-events: none;
	opacity: 0;

  transform: translateY( -3em );
	transition: transform 1000ms ease-out, opacity 1000ms ease-out;
}

/* Active */
.twinkler_layer > *.is_twinkling
{
	pointer-events: auto;
	opacity: 1;
  
	transform: translateY( 0 );
	transition-delay: 800ms;
}

/* Fading out */
.twinkler_layer > *.is_fading
{
	pointer-events: none;
	opacity: 0;
  
	transform: translateY( 5em );
	transition-timing-function: ease-in;
}

/*
.graduation_twinklers:hover .twinkler_layer > *
{
	opacity:1 !important;
	transform: none !important;
	outline: 1px solid cyan;
}
*/
              
            
!

JS

              
                
// --------------------------------------------------

var TWINKLER = function( $container , items )
{
	// Settings

  this.items_per_layer = 50;

	this.min_interval =  500; // ms
	this.max_interval = 1500; // ms

	// Storage

	this.items = items;
	this.$container = $container;

	this.layers = null;

	this.timer = null;

	this.slots = null;
	this.slot_cursor = null;

	// One-off initialisations

	this.sort_items_by_length();

	// Organise our layers
	this.layers = this.distribute_layers();
	
	// Build/refresh the DOM
	this.render();

	// Get ready...
	this.warm_up();

	// Go!
	this.cycle();
};

// --------------------------------------------------

TWINKLER.prototype.warm_up = function()
{
	// Setup

	var $layers = this.$container.querySelectorAll( ".twinkler_layer" );
	var items_per_layer = $layers[ 0 ].children.length;

	// Make an array of shuffled indexes, this is the order in which slots will be cycled

	var shuffled_index = [];
	for( var i = 1 ; i <= items_per_layer ; i ++ ) shuffled_index.push( i );

	var shuffle_cursor = items_per_layer;

	while( shuffle_cursor != 0 )
	{
		random_index = Math.floor( Math.random() * shuffle_cursor ); 
		shuffle_cursor--;

		var temp = shuffled_index[ shuffle_cursor ];
		shuffled_index[ shuffle_cursor ] = shuffled_index[ random_index ];
		shuffled_index[ random_index ] = temp;
	}

	// Make a big array of all the slots with their current index
	
	this.slots = [];

	for( var s = 0 ; s < shuffled_index.length ; s ++ )
	{
		var slot =
		{
			index: 0,
			$items: this.$container.querySelectorAll( ".twinkler_layer > *:nth-child("+parseInt( shuffled_index[ s ] )+")" ),
		};

		this.slots.push( slot );
	}

	// Reset the slot cursor

	this.slot_cursor = 0;
}

// --------------------------------------------------

TWINKLER.prototype.cycle = function()
{
	// Get the current slot deets
	var slot = this.slots[ this.slot_cursor ];

	// Get the current item index
	var item_index = slot.index;

	// Get the next item index, skipping empties
	var next_item_index = item_index;
	do { next_item_index = ( next_item_index + 1 ) % slot.$items.length; }
	while( slot.$items[ next_item_index ].innerHTML == '' )

	// Update the big old slots array
	slot.index = next_item_index;

	// Get a delay for the next transition
	var delay = parseInt( this.min_interval + Math.round( Math.random() * ( this.max_interval - this.min_interval ) ) );

	// Progress the slot cursor
	this.slot_cursor = ( this.slot_cursor + 1 ) % this.slots.length;

	this.timer = setTimeout( this.cycle.bind( this ) , delay );

	setTimeout( function(){
		slot.$items[ item_index ].classList.add( "is_fading" );
		slot.$items[ item_index ].classList.remove( "is_twinkling" );
		slot.$items[ next_item_index ].classList.add( "is_twinkling" );
	} , delay );

	setTimeout( function(){
		slot.$items[ item_index ].classList.remove( "is_fading" );
	} , parseInt( delay + 3000 ) );
}

// --------------------------------------------------

TWINKLER.prototype.render = function()
{
	var number_of_layers = this.layers.length;

	// Create somewhere to build the layers
	
	var $layers_staging = [];
	
	for( var l = 0 ; l < number_of_layers ; l ++ )
	{
		// Make the layer container
		var $layer = document.createElement( 'div' );
		$layer.classList.add( 'twinkler_layer' );
	
		$layers_staging.push( $layer );
	}
	
	// Create the items in each layer
	for( var i = 0 ; i < this.layers[ 0 ].length ; i ++ )
	{
		// Get the max length of the items at this index
		var max_length = 0;
		for( var l = 0 ; l < number_of_layers ; l ++ )
		{
			if( this.layers[ l ][ i ] ) max_length = Math.max( max_length , this.layers[ l ][ i ].length );
		}
	
		// Use the max length to calculate a width for these items
		var css_width = parseFloat( ( max_length * 0.7 ) + 0.1 ) + "em";
	
		// Create the item in each layer
		for( var l = 0 ; l < number_of_layers ; l ++ )
		{
			var $item = document.createElement( 'div' );
			$item.style.flexBasis = css_width;
			$item.innerHTML = this.layers[ l ][ i ];
	
			// Items in the first layer start visible
			if( l == 0 ) $item.classList.add( 'is_twinkling' );
	
			// Add to the staging area
			$layers_staging[ l ].appendChild( $item );
		}
	}
	
	// Empty our container first

	this.$container.innerHTML = '';

	// Add our layers to the DOM
	
	for( var l = 0 ; l < number_of_layers ; l ++ )
	{
		this.$container.appendChild( $layers_staging[ l ] );
	}

}

// --------------------------------------------------
// Sorts all items by name length

TWINKLER.prototype.sort_items_by_length = function()
{
	this.items.sort( function( a , b )
	{
		if( b.length == a.length )
		{
			return ( Math.random() * 2 ) - 1;
		}
		else return a.length - b.length;
	} );
}

// --------------------------------------------------
// Splits the one array into several layer arrays with synced item lengths

TWINKLER.prototype.distribute_layers = function()
{
	var number_of_layers = Math.ceil( this.items.length / this.items_per_layer );

	// Set up empty containers for our layers

	var layers = [];

	for( var l = 0 ; l < number_of_layers ; l ++ )
	{
		layers[ l ] = [];
	}

	// Distribute the items into their layers

	for( var i = 0 ; i < this.items.length ; i += number_of_layers )
	{
		for( var l = 0 ; l < number_of_layers ; l ++ )
		{
			layers[ l ].push( this.items[ i + l ] || "" );
		}
	}

	// Randomise the order of each layer

	var cursor = layers[ 0 ].length;

	while( cursor != 0 )
	{
		random_index = Math.floor( Math.random() * cursor ); 
		cursor--;

		// Swap items in each layer
		for( var l = 0 ; l < number_of_layers ; l ++ )
		{
			var swap = layers[ l ][ cursor ];
			layers[ l ][ cursor ] = layers[ l ][ random_index ];
			layers[ l ][ random_index ] = swap;
		}
	}

	return layers;
}

// --------------------------------------------------


		// --------------------------------------------------
		// Shuffle everything first
		
		var cursor = all_items.length;

		while( cursor != 0 )
		{
			random_index = Math.floor( Math.random() * cursor ); 
			cursor--;
			
			var temp = all_items[ cursor ];
			all_items[ cursor ] = all_items[ random_index ];
			all_items[ random_index ] = temp;
		}
		
		// --------------------------------------------------
		// Split into 2 arrays
		
		var midpoint = Math.ceil( all_items.length / 2 );
		
		var items_1 = all_items.slice( 0 , midpoint );
		var items_2 = all_items.slice( midpoint ) ;
		
		// --------------------------------------------------
		// Go!
		
		var twinkler_1 = new TWINKLER( document.getElementById( 'twinkler_1' ) , items_1 );
		var twinkler_2 = new TWINKLER( document.getElementById( 'twinkler_2' ) , items_2 );
		
		// --------------------------------------------------
              
            
!
999px

Console