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.

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="container">
	
	<div class="status status--invisible">
		Nothing is visible, scroll!
	</div>
	
	<div class="item">Item 1</div>
	<div class="item">Item 2</div>
	<div class="item">Item 3</div>
	<div class="item">Item 4</div>
	<div class="item">Last item</div>
	
</div>
              
            
!

CSS

              
                // Intersect items
.item {
	margin-top: 200vh;
	padding: 50px 30px;
	font-weight: 500;
  font-size: 16px;
  text-align: center;
	color: #fff;
	background-color: #312671;
		
	&:nth-child(3) {
		padding: 100px 30px;
		background-color: #4cb6c3;
	}
	
	&:nth-child(4) {
		padding: 30px;
		background-color: #b92073;
	}	
	
	&:nth-child(5) {
		padding: 200px 30px;
		background-color: #d46e14;
	}
}

// Status box
.status {
	position: fixed;
	top: 0; 
	right: 0;
  padding: 15px 30px 12px;
  color: #fff;
	text-align: center;
	
	&--visible {
		background-color: #459845;
	}
	
	&--invisible {
		background-color: #cc4848;
	}
}

.container {
	height: 100vh;
	overflow: auto;
	font-size: 12px;
	font-weight: 200;
	text-transform: uppercase;
	letter-spacing: 2px;
	background-color: #09061B;
}

              
            
!

JS

              
                // See a super simple version here
// https://codepen.io/SimonEvans/pen/wrMzeP

const statusBox = document.querySelector('.status');
let intersectItem = document.querySelectorAll('.item'), i;
let visiblity = 'invisible';
let statusText = 'Nothing is visible, scroll!';

const iObserver = new IntersectionObserver(items => {

  // Intersecting? (is the element in the available viewport)
  if (items[0].isIntersecting) {
		
		// Log visibility
    console.log('Visible? Yes');

    // Time when intersect occured (relative to page load)
    console.log('When? After ' + milliToSeconds(items[0].time));

    // Position values
    console.log('Position:')
    console.log(items[0].boundingClientRect);
		
		// Element that is visible
		console.log(items[0].target);
		
		// Update status vars
		visiblity = 'visible';
    statusText = items[0].target.textContent + ' is visible';

  } else {
    console.log('Visible? No')
    visiblity = 'invisible';
    statusText = 'Nothing is visible, scroll!';
  }

  console.log('---------------');

  // Update status
  updateStatus(statusText);
});

// Loop through and observe intersect items
for (i = 0; i < intersectItem.length; ++i) {
  iObserver.observe(intersectItem[i]);
}

// Update visibility status
function updateStatus(statusText) {
  statusBox.innerHTML = statusText;
  statusBox.className = 'status status--' + visiblity;
}

// Milliseconds -> Seconds
function milliToSeconds(milli) {
  let seconds = ((milli % 60000) / 1000).toFixed(0);
  return seconds + ' secs';
}

              
            
!
999px

Console