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="scroller">
	<header>
		<div class="container">
			<h1>Intersection Observer Visualizer</h1>
			<p class="subheading">A simple visualizer tool to assist with using the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API">Intersection Observer API</a>.</p>
			<p>Adjust the <code>rootMargin</code> and <code>threshold</code> values and scroll the container. The two shaded boxes show where the elements’ <code>intersectionRatio</code> matches the <code>threshold</code> value*.</p>
			<aside class="header__aside">
				<p><small><strong>*Note: </strong><code>threshold</code> can optionally take an array instead of a number. For simplicity, this visualizer only includes a single value option.</small></p>
			</aside>
		</div>
	</header>
	<main>
		<div class="grid">
			<div class="grid__input-col">
				<aside class="input-group">
					<h3>Options</h3>
					<div class="input-group__row">
						<label class="input-group__label" for="margin">rootMargin: <span class="input-group__value" data-margin></span></label>
						<input class="input-group__range" type="range" id="margin" name="margin" min="-30" max="200" step="10" value="120" data-input="margin">
					</div>
					<div class="input-group__row">
						<label class="input-group__label" for="margin">threshold: <span class="input-group__value" data-threshold></span></label>
						<input class="input-group__range" type="range" id="threshold" name="threshold" min="0" max="1" step="0.1" value="10" data-input="threshold">
					</div>
					<div class="info" data-info aria-live="polite"></div>
				</aside>
			</div>

			<div class="grid__observer">
				<div class="outer-wrapper">
					<div class="wrapper" data-wrapper>
							<section>
								<div class="box" data-box="1">1</div>
							</section>
							<section>
								<div class="box" data-box="2">2</div>
							</section>
							<section>
								<div class="box" data-box="3">3</div>
							</section>
							<section>
								<div class="box" data-box="4">4</div>
							</section>
							<section>
								<div class="box" data-box="5">5</div>
							</section>
						</div>
				</div>
			</div>
		</div>
	</main>
</div>
<footer>&copy; Michelle Barker</footer>


              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;600&display=swap');

* {
	box-sizing: border-box;
}

:root {
	--b: 10px;
	--box: max(10rem, 20vmin);
	--outerPad: 12rem;
	--primaryColor: deeppink;
	--sliderColor: var(--primaryColor);
	--sliderBorder: white;
	--sliderThumbColor: white;
	
	--ltBg: #f2f4f5;
	--mdBg: #e1e6e8;
	--dkBg: #303947;
	--text: #1d2026;
}

body {
	font-family: 'Poppins', sans-serif;
	font-weight: 300;
	margin: 0;
	padding: 1rem 0;
	width: 100%;
	background-color: var(--ltBg);
	color: var(--text);
	
	@media (min-width: 50em) {
		font-size: 1.2rem;
		padding: 0;
	}
}

.scroller {
	@media (min-width: 75em) {
		@supports (scroll-snap-type: y mandatory) {
			height: 100vh;
			overflow-y: scroll;
			scroll-snap-type: y mandatory;

			> * {
				scroll-snap-align: start;
			}
		}
	}
}

h1 {
	@media (min-width: 60em) {
		font-size: 3rem;
	}
}

h1,
h3 {
	margin: 0 0 1rem;
}

p {
	max-width: 70ch;
	line-height: 1.6;
}

strong {
	font-weight: 600;
}

a {
	color: var(--primaryColor);
	transition: color 200ms;
	
	&:hover,
	&:focus {
		color: black;
	}
}

header {
	padding: 1rem 0 2rem;
	
	@media (min-width: 80em) {
		margin-bottom: 1rem;
		padding: 2rem 0;
	}
}

.header__aside {
	background-color: var(--mdBg);
	padding: 0.125rem 1rem;
	border-radius: 0.2rem;
	max-width: 70ch;
}

code {
	background-color: #cbd3d6;
	padding: 0.125rem;
}

.subheading {
	font-weight: 600;
	
	@media (min-width: 50em) {
		font-size: 1.4rem;
	}
}

.container {
	max-width: 120rem;
	margin: 0 auto;
	padding: 0 1rem;
	
	@media (min-width: 50em) {
		padding: 0 2rem;
	}
}

.grid {
	@media (min-width: 75em) {
		display: grid;
		grid-template-columns: 32rem 2fr;
		gap: 1rem;
		background-color: var(--mdBg);
		hright: 100vh;
	}
	
	@media (min-width: 100em) {
		grid-template-columns: 1fr 2fr;
		min-height: 100vh;
	}
}

.grid__observer {
	padding: var(--outerPad);
	background-color: var(--mdBg);
}

section {
	height: 85vh;
	max-height: 35rem;
	display: flex;
	align-items: center;
	justify-content: center;
}

.box {
	position: relative;
	width: var(--box);
	height: var(--box);
	background-color: rgba(112, 230, 214, 1);
	color: white;
	padding: 1rem;
	display: flex;
	align-items: center;
	justify-content: center;
	transition: background-color 200ms;
	counter-increment: section;
	
	&.is-inview {
		background-color: var(--primaryColor);
	}
	
	&::after {
		content: counter(section);
		position: absolute;
		top: 0.5rem;
		left: 50%;
		transform: translate3d(-50%, 0, 0);
		padding: 0.25rem;
		width: 1.2rem;
		height: 1.2rem;
		font-size: 0.85rem;
		border-radius: 50%;
		background-color: rgba(0, 0, 0, 0.2);
		display: flex;
		align-items: center;
		justify-content: center;
	}
}

.outer-wrapper {
	position: relative;
	width: 100%;
	max-width: 90vw;
	height: calc(80vh - 12rem);
	min-height: 25rem;
	max-height: 40rem;
	margin: 0 auto;
	outline: var(--b) solid paleturquoise;
	transition: outline 200ms;
	
	&::after,
	&::before {
		--y: calc(100% - var(--t));
		display: block;
		content: '';
		position: absolute;
		width: var(--box);
		height: var(--box);
		left: 50%;
		transform: translate3d(-50%, 0, 0);
		background: rgba(0, 0, 0, 0.25);
		pointer-events: none;
		transition: transform 200ms, top 200ms, bottom 200ms;
		will-change: top, left;
		z-index: 1;
	}
	
	&::before {
		--offsetY: calc(var(--y) * -1);
		top: calc(var(--b) * -1);
		transform: translate3d(-50%, var(--offsetY, 0), 0);
	}
	
	&::after {
		bottom: calc(var(--b) * -1);
		top: auto;
		transform: translate3d(-50%, var(--y, 0), 0) rotate(180deg);
	}
	
	@media (min-width: 75em) {
		max-width: 65rem;
	 	margin: 0 auto 0 0;
	}
}

.wrapper {
	width: 100%;
	height: 100%;
	overflow: scroll;
	border: 2px solid;
	outline-offset: var(--b);
	outline: 2px dashed var(--text);
	transition: outline-offset 200ms;
	background-color: white;
	counter-reset: section;
}

.grid__input-col {
	@media (min-width: 75rem) {
		background-color: var(--dkBg);
	}
}

.input-group {
	padding: 1rem;
	background-color: var(--dkBg);
	color: white;
	z-index: 1;
	
	@media (min-width: 50em) {
		font-size: 1rem;
	}
	
	@media (min-width: 75rem) {
		margin: var(--outerPad) 0 0;
		align-self: start;
		max-width: 31rem;
		margin-left: auto;
		padding-right: 3rem;
	}
}

.input-group__row {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	max-width: 31rem;
}

.input-group__label {
	display: flex;
	flex: 1 0 10rem;
	margin-right: 1rem;
}

.input-group__range {
	flex: 0 0 15rem;
}

.input-group__value {
	font-weight: 600;
	margin-left: auto;
}

.info {
	margin: 2rem 0 2rem;
	background-color: #1d2026;
	padding: 0.5rem 1rem;
	
	p {
		margin: 0;
	}
}

.info__elements {
	font-weight: 600;
	color: var(--primaryColor);
	padding-left: 0.5rem;
}

footer {
	background-color: var(--text);
	color: var(--mdBg);
	font-size: 0.9rem;
	padding: 0.5rem 1rem;
	
	@media (min-width: 50rem) {
		padding: 0.5rem 2rem;
	}
}


/* Range input styles (http://danielstern.ca) */
input[type=range] {
  -webkit-appearance: none;
  width: 100%;
  margin: 5px 0;
	background: var(--inputBg);
}

input[type=range]:focus {
  outline: none;
}

input[type=range]::-webkit-slider-runnable-track {
	width: 100%;
  height: 6px;
  cursor: pointer;
  background: var(--sliderColor);
}

input[type=range]::-webkit-slider-thumb {
  box-shadow: 0 0 0.4rem rgba(0, 0, 0, 0.5);
  border: 1px solid var(--sliderBorder);
  height: 20px;
  width: 20px;
  border-radius: 50%;
  background: var(--sliderThumbColor);
  cursor: pointer;
  -webkit-appearance: none;
  margin-top: -7px;
}

input[type=range]:focus::-webkit-slider-runnable-track {
  background: #8080ff;
}


input[type=range]::-moz-range-track {
  width: 100%;
  height: 6px;
  cursor: pointer;
  background: var(--sliderColor);
}

input[type=range]::-moz-range-thumb {
  box-shadow: 0 0 0.4rem rgba(0, 0, 0, 0.5);
  border: 1px solid var(--sliderBorder);
  height: 20px;
  width: 20px;
  border-radius: 50%;
  background: var(--sliderThumbColor);
  cursor: pointer;
}

input[type=range]::-ms-track {
  width: 100%;
  height: 6px;
  cursor: pointer;
  background: transparent;
  border-color: transparent;
  color: transparent;
}

input[type=range]::-ms-fill-lower {
  background: var(--sliderColor);
  border: 0px solid var(--sliderBorder);;
}

input[type=range]::-ms-fill-upper {
  background: var(--sliderColor);
  border: 0px solid var(--sliderBorder);
}

input[type=range]::-ms-thumb {
  box-shadow: 0 0 0.4rem rgba(0, 0, 0, 0.5);
  border: 1px solid var(--sliderBorder);
  height: 20px;
  width: 20px;
  border-radius: 21px;
  background: var(--sliderThumbColor);
  cursor: pointer;
}

input[type=range]:focus::-ms-fill-lower {
  background: #8080ff;
}

input[type=range]:focus::-ms-fill-upper {
  background: #8080ff;
}


              
            
!

JS

              
                const marginInput = document.querySelector('[data-input="margin"]')
const thresholdInput = document.querySelector('[data-input="threshold"]')
const wrapper = document.querySelector('[data-wrapper]')
const boxes = [...document.querySelectorAll('[data-box]')]
const marginText = document.querySelector('[data-margin]')
const thresholdText = document.querySelector('[data-threshold]')
const info = document.querySelector('[data-info]')

const getOptions = () => {
	return {
		root: wrapper,
		rootMargin: `${marginInput.value}px`,
		threshold: parseFloat(thresholdInput.value)
	}
}

let elementsInView = []

const getInfo = () => {
	if (elementsInView.length) {
		return `<p>Element indexes in view: <span class="info__elements">${elementsInView.join(', ')}</span></p>`
	} else {
		return '<p>No elements in view</p>'
	}
}

const addElementToInfoBox = (target) => {
	if (!elementsInView.includes(target.dataset.box)) {
		elementsInView.push(target.dataset.box)
	}
	
	return info.innerHTML = getInfo()
}

const removeElementFromInfoBox = (target) => {
	elementsInView = elementsInView.filter(el => el !== target.dataset.box)
	
	return info.innerHTML = getInfo()
}

const setInViewStyles = (target) => {
	target.textContent = 'In view'
	target.classList.add('is-inview')
	addElementToInfoBox(target)
	// console.log('in view')
}

const setOutOfViewStyles = (target) => {
	target.textContent = ''
	target.classList.remove('is-inview')
	removeElementFromInfoBox(target)
	// console.log('out of view')
}

const onIntersect = (entries) => {
	entries.forEach(entry => {
		const { target, isIntersecting, intersectionRatio } = entry
		console.log(entry)
		
		if (intersectionRatio >= thresholdInput.value && isIntersecting) {
			return setInViewStyles(target)
		}
		
		return setOutOfViewStyles(target)
	})
}

const reinitObserver = (options) => {
	if (observer) {
		observer.disconnect()
	}
	
	setTimeout(() => {
		observer = new IntersectionObserver(onIntersect, options)
	
		boxes.forEach(el => {
			observer.observe(el)
		})
	}, 100)
}

const setMargin = (marginValue) => {
	const newValue = `${marginInput.value}px`
	document.body.style.setProperty('--b', marginValue)
	marginText.textContent = marginValue
}

const setThreshold = (thresholdValue) => {
	const tValue = `${thresholdValue * 100}%`
	thresholdText.textContent = thresholdValue
	document.body.style.setProperty('--t', tValue)
}

const onChangeThreshold = () => {
	const opts = getOptions()
	setThreshold(opts.threshold)
	reinitObserver(opts)
}

const onChangeMargin = () => {
	const opts = getOptions()
	setMargin(opts.rootMargin)
	reinitObserver(opts)
}

let observer = new IntersectionObserver(onIntersect, getOptions())

boxes.forEach(el => {
	observer.observe(el)
})

setMargin(getOptions().rootMargin)
setThreshold(getOptions().threshold)
marginInput.addEventListener('change', onChangeMargin)
thresholdInput.addEventListener('change', onChangeThreshold)


              
            
!
999px

Console