<!doctype html>
<html>
	<head>
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<link href="/assets/reset.css" rel="stylesheet">
		<link href="setup.css" rel="stylesheet">
		<link href="style.css" rel="stylesheet">
		<script defer src="script.js"></script>
	</head>
	<body>
		<section>
			<p>I’ve just added some text here to have another element, and also added some CSS to style it a bit—nothing too fancy.</p>
		</section>
		<section>
			<p>I’ve just added some text here to have another element, and also added some CSS to style it a bit—nothing too fancy.</p>
		</section>
		<section>
			<p>I’ve just added some text here to have another element, and also added some CSS to style it a bit—nothing too fancy.</p>
		</section>
		<section>
			<p>I’ve just added some text here to have another element, and also added some CSS to style it a bit—nothing too fancy.</p>
		</section>
		<section>
			<p>I’ve just added some text here to have another element, and also added some CSS to style it a bit—nothing too fancy.</p>
		</section>
		<section>
			<p>I’ve just added some text here to have another element, and also added some CSS to style it a bit—nothing too fancy.</p>
		</section>
		<section>
			<p>I’ve just added some text here to have another element, and also added some CSS to style it a bit—nothing too fancy.</p>
		</section>
		<section>
			<p>I’ve just added some text here to have another element, and also added some CSS to style it a bit—nothing too fancy.</p>
		</section>
		<section>
			<p>I’ve just added some text here to have another element, and also added some CSS to style it a bit—nothing too fancy.</p>
		</section>
		<section>
			<p>I’ve just added some text here to have another element, and also added some CSS to style it a bit—nothing too fancy.</p>
		</section>
		<section>
			<p>I’ve just added some text here to have another element, and also added some CSS to style it a bit—nothing too fancy.</p>
		</section>
		<section>
			<p>I’ve just added some text here to have another element, and also added some CSS to style it a bit—nothing too fancy.</p>
		</section>
		<section>
			<p>I’ve just added some text here to have another element, and also added some CSS to style it a bit—nothing too fancy.</p>
		</section>
		<section>
			<p>I’ve just added some text here to have another element, and also added some CSS to style it a bit—nothing too fancy.</p>
		</section>
		<section>
			<p>I’ve just added some text here to have another element, and also added some CSS to style it a bit—nothing too fancy.</p>
		</section>
		<section>
			<p>I’ve just added some text here to have another element, and also added some CSS to style it a bit—nothing too fancy.</p>
		</section>
		<section>
			<p>I’ve just added some text here to have another element, and also added some CSS to style it a bit—nothing too fancy.</p>
		</section>
		<section>
			<p>I’ve just added some text here to have another element, and also added some CSS to style it a bit—nothing too fancy.</p>
		</section>
		<section>
			<p>I’ve just added some text here to have another element, and also added some CSS to style it a bit—nothing too fancy.</p>
		</section>
	</body>
</html>

		
index.html
			let highlightClass = 'highlight' // Set up variables again.
let highlightBlocks = document.querySelectorAll('section') // Gets all of them.

// Loop through the list, doing this `forEach` one.
highlightBlocks.forEach((block) => {
	let sectionObserver = new IntersectionObserver(([entry])=> {
		// When it is intersecting, apply the class; otherwise, remove it.
		if (entry.isIntersecting) {
			block.classList.add(highlightClass)
		} else {
			block.classList.remove(highlightClass)
		}
	}, {
		root: document, // This is only needed here in the example `iframe`!
		rootMargin: '-25% 0% -25% 0%', // CSS-ish: top/right/bottom/left.
	})

	sectionObserver.observe(block) // Watch each one!
})

		
script.js
			body {
	--base: 1rem;

	display: grid;
	font-family: sans-serif;
	padding: var(--base);
	row-gap: var(--base);
}

		
setup.css
			section { /* Initial state, without the class. */
	background-color: gold;
	padding: calc(var(--base) / 2);
	transition: all 1s ease-in-out; /* Transition everything. */
	will-change: transform; /* Improves the aliasing! */

	&.highlight { /* When the class is applied with JS! */
		background-color: aquamarine;
		transform: rotate(5deg);
	}
}

		
style.css