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 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

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

              
                <input id="play" type="checkbox" /><label for="play">Set Scroll Position to Bottom</label>
<input id="reversal" type="checkbox" checked="checked" /><label for="reversal">Revert items to original position when scrolling up again</label>
<input id="overflow" type="checkbox" /><label for="overflow">Hide overflowing content (Firefox fix)</label>

<label for="play" class="controlbutton" id="forwardbutton"></label>
<label for="play" class="controlbutton" id="backwardbutton"></label>

<div id="browser">
	<div id="progress"></div>
	<main>
		<h1>Lorem ipsum dolor</h1>
		<p>Sit amet, consectetur adipiscing elit. Nunc lectus eros, dapibus quis egestas eget, commodo et leo.</p>
		<p>Nulla est ipsum, elementum a neque a, maximus ultricies justo. Nulla tincidunt nec nulla eu malesuada. Etiam maximus massa nec auctor facilisis.</p>
		<p>Henean sed commodo nibh, porttitor efficitur nisl. Integer porta, libero at facilisis consectetur, velit lorem posuere enim, vulputate vulputate massa ante sed diam.</p>
		<h2>Morbi aliquam efficitur finibus.</h2>
		<p>Phasellus ornare ultricies purus vel hendrerit. Suspendisse id ultricies nisi. Pellentesque rutrum ante ut rhoncus gravida.</p>
		<p>Henean sed commodo nibh, porttitor efficitur nisl. Integer porta, libero at facilisis consectetur, velit lorem posuere enim, vulputate vulputate massa ante sed diam.</p>
		<p>Nulla est ipsum, elementum a neque a, maximus ultricies justo. Nulla tincidunt nec nulla eu malesuada. Etiam maximus massa nec auctor facilisis.</p>
		<h2>Morbi aliquam.</h2>
		<p>Henean sed commodo nibh, porttitor efficitur nisl. Integer porta, libero at facilisis consectetur, velit lorem posuere enim, vulputate vulputate massa ante sed diam.</p>
		<h3>Morbi aliquam efficitur finibus.</h3>
		<p>Phasellus ornare ultricies purus vel hendrerit.</p>
	</main>
	<aside id="scrollbar">
		<div id="thumb"></div>
	</aside>
</div>

<details>
	<summary>ℹ️</summary>
	<dialog open>
		<h1>About this Pen</h1>
		<p>ℹ️ Scroll-Triggered Animations are animations that are triggered when scrolling past a certain position. Once triggered, these animations start and finish on their own, independent of any future scroll position.</p>

		<p>👉 A typical example is content that flies/fades in as you scroll across the page. Once it's in view the animation starts and there's no stopping it.</p>
		
		<p>☑️ In this demo you can't scroll yourself, but can fake a scroll using the “Set Scroll Position to Bottom” control at the top left.</p>
		
		<p>🤔 Want to learn more about Scroll-Linked Animations / Scroll-Timeline? Check out these two articles:</p>
		
		<ul>
			<li><a href="https://brm.us/scroll-linked-animations-pt1" target="_top">Scroll-Linked Animations with <code>@scroll-timeline</code> (Part 1) — Introduction</a></li>
			<li><a href="https://brm.us/scroll-linked-animations-pt2" target="_top">Scroll-Linked Animations with <code>@scroll-timeline</code> (Part 2) — Element-Based Offsets</a></li>
		</ul>

		<p>👀 More animations/visualizations: <a href="https://codepen.io/collection/AYoego" target="_top">https://codepen.io/collection/AYoego</a></p>
		
		<p>👋 Built by Bramus! — <a href="https://www.twitter.com/bramus" target="_top">@bramus</a> - <a href="https://www.bram.us/" target="_top">https://www.bram.us/</a> </p>
	</dialog>
</details>
              
            
!

CSS

              
                :root {
	--browser-size--ul: 35; /* in vmin */
	--content-size--ul: calc(var(--browser-size--ul) * 3.1);
	--thumb-size--ul: calc(
		var(--browser-size--ul) / var(--content-size--ul) * var(--browser-size--ul)
	);

	--browser-size: calc(var(--browser-size--ul) * 1vmin);
	--content-size: calc(var(--content-size--ul) * 1vmin);
	--thumb-size: calc(var(--thumb-size--ul) * 1vmin);

	--scrollbar-width: 1vmin;
}

html,
body {
	width: 100vw;
	height: 100vh;
	display: grid;
	place-items: center;
	overflow: hidden;
	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
}

#browser {
	height: var(--browser-size);
	width: calc(var(--browser-size) * 4 / 3);

	outline: 10px solid lightblue;

	display: grid;
	grid-template-columns: minmax(0, 1fr) var(--scrollbar-width);
	grid-template-rows: minmax(0, 1fr);
	grid-template-areas: "content scrollbar";

	position: relative;
}

#browser main, #thumb {
	transition: transform 3s linear, margin-top 3s linear;
}

main {
	font-family: "BLOKK";
	font-size: calc(2vmin + .2em);
	padding: 1rem;
	color: #999;

	grid-area: content;
	height: var(--content-size);
	overflow: hidden;

	outline: 1px solid grey;
	z-index: -1;

	will-change: transform;
}

main > * {
	opacity: 0;
	transition: all 0.2s ease-in-out;
	transform: translateX(calc(var(--browser-size) * 4 / 3 * -0.8));
}

main > .in-view {
	opacity: 1;
	transform: translateX(0);
}

#scrollbar {
	grid-area: scrollbar;
	height: var(--browser-size);
	align-self: start;

	background: #ccc;
}

#scrollbar #thumb {
	top: 0;
	height: var(--thumb-size);
	width: 100%;
	background: #333;
	border-radius: 9999px;

	will-change: transform;
}

/* Controls */
.controlbutton {
	position: fixed;
	top: 50%;
	left: 50%;
	z-index: 2;
	transform: translateX(calc(-50% + var(--offset))) translateY(calc(-4rem + (var(--browser-size) / -2)));
}

#forwardbutton {
	--offset: 75%;
}

#backwardbutton {
	--offset: -75%;
}

.controlbutton::after {
	font-size: 2em;
	cursor: pointer;
}

#forwardbutton::after {
	content: '⏭';
}

#backwardbutton::after {
	content: '⏮';
}

#play ~ #forwardbutton,
#play:checked ~ #backwardbutton {
	opacity: 1;
	pointer-events: auto;
}

#play:checked ~ #forwardbutton,
#play ~ #backwardbutton{
	opacity: 0.5;
	pointer-events: none;
}

/* Checkboxes */
#play,
#reversal,
#overflow,
#play + label,
#reversal + label,
#overflow + label {
	position: fixed;
	left: 1rem;
	margin: 0;
}
#play + label,
#reversal + label,
#overflow + label {
	left: 2.25rem;
	white-space: nowrap;
}

#play,
#play + label {
	top: 1rem;
}
#reversal,
#reversal + label {
	top: 2.5rem;
}
#overflow,
#overflow + label {
	top: 4rem;
}

/* Respond too checkboxes */
#play:checked ~ #browser main {
	transform: translateY(calc(-100% + var(--browser-size)));
}

#play:checked ~ #browser #thumb {
	/* 🪓 Hack: we animate margin-top to force repaints so that 
	   our IntersectionObserver will kick into action … 
	*/
	margin-top: calc(var(--browser-size) - var(--thumb-size));
}

#overflow:checked ~ #browser {
	overflow: hidden;
}

/** Poor man's infobox */
details {
	position: fixed;
	bottom: 1em;
	right: 1em;
	font-size: clamp(1.25rem, 2vw, 2rem);
	z-index: 9999;
}
dialog[open] {
	position: fixed;
	top: 10vmin;
	right: 10vmin;
	bottom: 10vmin;
	left: 10vmin;
	width: auto;
	height: auto;

	/* Extra CSS for Safari … */
	background: #eee;
	border: 0.25em solid lightblue;
	padding: 1em;
	overflow: auto;
}
details[open] > summary::after {
	content: '';
	position: fixed;
	top: 0; right: 0; bottom: 0; left: 0;
	background: rgba(0 0 0 / 0.5);
	display: block;
}

/* Hide summary arrow …*/
details summary::-webkit-details-marker {
	display: none;
}
details summary {
	display: inline-block;
}
details summary::marker {
	display: none;
}

              
            
!

JS

              
                window.addEventListener('DOMContentLoaded', () => {

	const observer = new IntersectionObserver(entries => {
		entries.forEach(entry => {
			if (entry.isIntersecting) {
				entry.target.classList.add('in-view');
			} else {
				if (document.getElementById('reversal').checked) {
					entry.target.classList.remove('in-view');
				}
			}
		});
	}, {
		root: document.querySelector('#browser'),
		rootMargin: '1000% 0% -20% 0%', // 🪓 Hack: Set top margin to 1000% so that elements which slide out from the top are considered to be intersecting. That way their .in-view class is retained.
	});

	// Track all sections that have an `id` applied
	document.querySelectorAll('main > *').forEach((section) => {
		observer.observe(section);
	});
	
});
              
            
!
999px

Console