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;

  padding: 0 0.5rem;
  
	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;
}

/* Active */
.twinkler_layer > *.is_twinkling
{
	pointer-events: auto;
	opacity: 1;
}

              
            
!

JS

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

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

	this.items_per_layer = 50;

	// Storage

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

	this.layers = null;

	// One-off initialisations

	this.sort_items_by_length();

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

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

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 ++ )
	{
		// Create the item in each layer
		for( var l = 0 ; l < number_of_layers ; l ++ )
		{
			var $item = document.createElement( 'div' );
			$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