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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

            
              <main>
	<section>
		<h2>Section One</h2>
	</section>
	<section>
		<h2>Section Two</h2>
	</section>
	<section>
		<h2>Section Three</h2>
	</section>
	<section>
		<h2>Section Four</h2>
	</section>
	<section>
		<h2>Section Five</h2>
	</section>
	<section>
		<h2>Section Six</h2>
	</section>
	<section>
		<h2>Section Seven</h2>
	</section>
	<section>
		<h2>Section Eight</h2>
	</section>
	<section>
		<h2>Section Nine</h2>
	</section>
	<section>
		<h2>Section Ten</h2>
	</section>
	<section>
		<h2>Section Eleven</h2>
	</section>
	<section>
		<h2>Section Twelve</h2>
	</section>
</main>
            
          
!

CSS

            
              $maxOpacity: 1.0;
$minOpacity: 0.5;

// Start demo setup
@mixin checkered-background {
	background: {
		color: #eee;
		image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="400" height="400" fill-opacity=".25"><rect x="200" width="200" height="200" /><rect y="200" width="200" height="200" /></svg>');
		size: 30px 30px;
	}
}
@mixin demo_setup {
	html, * {
		font-family: sans-serif;
	}

	body {
		@include checkered-background;
	}

	section {
		background-color: rgb(193, 17, 170);
		margin: 0.5rem;
		height: 10vh;
		position: relative;
	}

	h2	{
		position: absolute;
		top: 50%;
		transform: translateY(-50%);
		text-align: center;
		color: white;
		width: 100%;
		left: 0;
		font-size: 1rem;
	}
}
// End Demo Setup

@include demo_setup;

section {
	opacity: $maxOpacity; // Fallback until JS is running
	transition: 0.5s opacity;
	.js & {
		opacity: $minOpacity;
	}
}

            
          
!

JS

            
              const maxOpacity = 1; // Fully highlighted elements
const minOpacity = 0.5; // Defocused elements
const screenMargin = 0.25; // Top and bottom threshold to trigger transition
const sections = document.querySelectorAll('section');

/**
 * Identifies if the element is within the defined display area
 * @param {Object}
 * @returns {Boolean}
 **/
const isOnScreen = (el) => {
	const elRects = el.getClientRects()[0];
	const winHeight = window.innerHeight;
	// Top of object is above bottom of viewport margin
	const topIsOnscreen =	(elRects.top < (winHeight * (1 - screenMargin)));
	// Bottom of object is below the top of viewport margin
	const bottomIsOnscreen = (elRects.bottom > (winHeight * screenMargin));

	return (topIsOnscreen && bottomIsOnscreen);
}

/**
 * Sets the correct opacity for all elements and
 * iterates to the next animation frame
 **/
const update = () => {
	sections.forEach( (section) => {
		let opacity = minOpacity;
		if(isOnScreen(section)) {
			 opacity = maxOpacity;
		}
		section.style.opacity = opacity;
	});

	window.requestAnimationFrame(update);
}

/**
 * Starts the sequence
 **/
const init = () => {
	// Set CSS
	document.querySelector('body').classList.add('js');
	
	// Start the effects
	update();
}

init();

            
          
!
999px

Console