Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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 id="scroll-container">
	<div id="indicator"></div>
</div>

<article>
	<h1 id="header-1">Scroll Indicator</h1>

	<p>This is a page scroll indicator inspired by the one on <a href="https://lesswrong.com">LessWrong</a>. Headers are noted and immediately jumpable, and it has two possible appearances that swap out based on breakpoints for a responsive experience best tailored to the user.</p>
	<p>On viewports with sufficient horizontal space, the indicator is displayed vertically and has a thumb that represents the height of the user's viewport. On narrower viewports, the indicator manifests as a horizontal progress bar at the top of the screen.</p>
	<p>Ideally, the base of this effect could be achieved using scroll-driven animations with additional JavaScript-based functionality considered progressive enhancement, but that has restrictions if the page has content beyond the main article, such as comments, where one must start implementing <code>overflow: scroll</code> hackery.</p>

	<h2 id="header-2">Header 2</h2>
	<p>Deserunt molestias esse occaecati. Et nesciunt ad inventore tenetur quos quisquam. Adipisci labore nemo rerum rerum sit doloribus blanditiis quia. Voluptatem sequi odit ea repellendus. Quasi vero cupiditate quia esse.</p>

	<h3 id="header-3">Header 3</h3>
	<p>Deserunt molestias esse occaecati. Et nesciunt ad inventore tenetur quos quisquam. Adipisci labore nemo rerum rerum sit doloribus blanditiis quia. Voluptatem sequi odit ea repellendus. Quasi vero cupiditate quia esse.</p>
	<p>Deserunt molestias esse occaecati. Et nesciunt ad inventore tenetur quos quisquam. Adipisci labore nemo rerum rerum sit doloribus blanditiis quia. Voluptatem sequi odit ea repellendus. Quasi vero cupiditate quia esse.</p>
	<p>Deserunt molestias esse occaecati. Et nesciunt ad inventore tenetur quos quisquam. Adipisci labore nemo rerum rerum sit doloribus blanditiis quia. Voluptatem sequi odit ea repellendus. Quasi vero cupiditate quia esse.</p>
	<p>Deserunt molestias esse occaecati. Et nesciunt ad inventore tenetur quos quisquam. Adipisci labore nemo rerum rerum sit doloribus blanditiis quia. Voluptatem sequi odit ea repellendus. Quasi vero cupiditate quia esse.</p>
	<p>Deserunt molestias esse occaecati. Et nesciunt ad inventore tenetur quos quisquam. Adipisci labore nemo rerum rerum sit doloribus blanditiis quia. Voluptatem sequi odit ea repellendus. Quasi vero cupiditate quia esse.</p>
	<p>Deserunt molestias esse occaecati. Et nesciunt ad inventore tenetur quos quisquam. Adipisci labore nemo rerum rerum sit doloribus blanditiis quia. Voluptatem sequi odit ea repellendus. Quasi vero cupiditate quia esse.</p>
	<p>Deserunt molestias esse occaecati. Et nesciunt ad inventore tenetur quos quisquam. Adipisci labore nemo rerum rerum sit doloribus blanditiis quia. Voluptatem sequi odit ea repellendus. Quasi vero cupiditate quia esse.</p>

	<h2 id="header-4">Header 4</h2>
	<p>Deserunt molestias esse occaecati. Et nesciunt ad inventore tenetur quos quisquam. Adipisci labore nemo rerum rerum sit doloribus blanditiis quia. Voluptatem sequi odit ea repellendus. Quasi vero cupiditate quia esse.</p>
	<p>Deserunt molestias esse occaecati. Et nesciunt ad inventore tenetur quos quisquam. Adipisci labore nemo rerum rerum sit doloribus blanditiis quia. Voluptatem sequi odit ea repellendus. Quasi vero cupiditate quia esse.</p>
</article>

<section>
	<h2>Other, non-article content</h2>
	<p>This content shouldn't be shown in the scrollbar</p>
	<p>Deserunt molestias esse occaecati. Et nesciunt ad inventore tenetur quos quisquam. Adipisci labore nemo rerum rerum sit doloribus blanditiis quia. Voluptatem sequi odit ea repellendus. Quasi vero cupiditate quia esse.</p>
</section>
              
            
!

CSS

              
                :root {
	color-scheme: light dark;

	--black: oklch(18% 0.003 17.5);

	--dark_grey: oklch(24% 0.006 17.6);
	--grey: oklch(36% 0.003 17.3);
	--bright_grey: oklch(47% 0.005 17.3);

	--white: oklch(76% 0.03 55);
	--bright_white: oklch(94.75% 0.04 73);

	--blue: oklch(56.5% 0.13 253);
	--cyan: oklch(71.2% 0.112 198.7);
	--magenta: oklch(61.2% 0.21 5.4);
}

body {
	margin: 3rem 3rem 3rem 5rem;
	font-family: "EB Garamond", serif;
	color: light-dark(var(--black), var(--bright_white));
	background: light-dark(var(--bright_white), var(--black));

	@media (max-width: 45rem) {
		margin: 0 0.8rem;
	}
}

article {
	max-width: 80ch;

	* + h2,
	* + h3 {
		margin-top: 2rem;
	}

	> * + * {
		margin-top: 1rem;
	}
}

h1,
h2,
h3,
h4,
h5,
h6 {
	text-transform: uppercase;
	text-wrap: balance;
	font-weight: 700;
}

h1 {
	font-size: 2.5rem;
	line-height: 2.8rem;
}

h2 {
	font-size: 1.8rem;
	line-height: 2rem;
}

h3 {
	font-size: 1.2rem;
	line-height: 1.4rem;
}

a {
	color: inherit;
	text-decoration: underline 0.1rem light-dark(var(--cyan), var(--blue));
	-webkit-text-decoration-line: underline;
	-webkit-text-decoration-color: light-dark(var(--cyan), var(--blue));
	text-decoration-skip-ink: none;
	text-underline-offset: 0.2rem;

	&:hover,
	&:active {
		text-decoration-thickness: 0.7rem;
		text-underline-offset: -0.37rem;
	}
}

code {
	font-size: 80%;
}

#scroll-container {
	display: none; /* Set to block in JS */
	position: fixed;
	inset: 2rem auto 2rem 1rem;
	width: 2px;
	background-color: light-dark(var(--white), var(--grey));
	z-index: 1000;

	#indicator {
		position: absolute;
		left: -0.5px;
		width: 3px;
		background-color: var(--magenta);
		min-height: 5%;
	}

	.heading-indicator {
		position: absolute;
		left: -0.5px;
		width: 3px;
		height: 5px;
		background-color: light-dark(var(--bright_white), var(--black));

		&:hover::after {
			content: attr(title);
			position: absolute;
			left: 15px;
			top: 50%;
			background-color: var(--bright_grey);
			padding: 4px 10px;
			white-space: nowrap;
		}
	}
}

@media (max-width: 45rem) {
	#scroll-container {
		inset: 0 0 auto;
		height: 4px;
		width: 100%;

		#indicator {
			left: 0;
			height: 100%;
		}

		.heading-indicator {
			width: 5px;
			height: 4px;
		}
	}
}

              
            
!

JS

              
                class ScrollProgressIndicator {
	constructor() {
		this.scrollContainer = document.getElementById("scroll-container");
		this.indicator = document.getElementById("indicator");
		this.article = document.querySelector("article");
		this.headers = this.article.querySelectorAll("h2");
		this.breakpoint = 700;
		this.currentLayout = null;

		this.init();
	}

	init() {
		if (!this.article) {
			console.warn("No article element found");
			return;
		}

		this.scrollContainer.style.display = "block";
		this.setupEventListeners();
		this.update();
	}

	setupEventListeners() {
		document.addEventListener("scroll", () => this.update());
		document.addEventListener("DOMContentLoaded", () => this.update());
		window.addEventListener("resize", () => this.update());
	}

	getArticleDimensions() {
		const articleRect = this.article.getBoundingClientRect();
		const scrollTop = window.scrollY - this.article.offsetTop;
		const adjustedScrollTop = Math.max(
			0,
			Math.min(scrollTop, this.article.scrollHeight)
		);

		return {
			articleTop: articleRect.top + window.scrollY,
			articleHeight: this.article.scrollHeight,
			articleBottom: articleRect.bottom + window.scrollY,
			viewportHeight: window.innerHeight,
			viewportWidth: window.innerWidth,
			scrollTop: adjustedScrollTop
		};
	}

	isScrolledIntoArticle() {
		const { articleTop, articleBottom } = this.getArticleDimensions();
		const viewportTop = window.scrollY;
		const viewportBottom = viewportTop + window.innerHeight;

		return viewportBottom >= articleTop && viewportTop <= articleBottom;
	}

	createHeaderMarker(header, position, isHorizontal) {
		const line = document.createElement("a");
		line.href = `#${header.id}`;
		line.className = "heading-indicator";
		line.setAttribute("title", header.textContent);

		if (isHorizontal) {
			line.style.left = `${position}%`;
			line.style.top = "";
		} else {
			line.style.top = `${position}%`;
			line.style.left = "";
		}

		return line;
	}

	resetIndicatorStyles() {
		this.indicator.style.width = "";
		this.indicator.style.height = "";
		this.indicator.style.top = "";
		this.indicator.style.left = "";
	}

	updateIndicator(isHorizontal) {
		const {
			articleHeight,
			viewportHeight,
			scrollTop
		} = this.getArticleDimensions();

		if (isHorizontal) {
			const width = (scrollTop / (articleHeight - viewportHeight)) * 100;
			const clampedWidth = Math.max(0, Math.min(100, width));
			this.indicator.style.width = `${clampedWidth}%`;
			this.indicator.style.left = "";
			this.indicator.style.top = "";
		} else {
			const height = (viewportHeight / articleHeight) * 100;
			const position = (scrollTop / articleHeight) * 100;
			const clampedPosition = Math.max(0, Math.min(100 - height, position));
			this.indicator.style.height = `${height}%`;
			this.indicator.style.top = `${clampedPosition}%`;
			this.indicator.style.left = "";
		}
	}

	updateHeaderMarkers(isHorizontal) {
		const { articleHeight, articleTop } = this.getArticleDimensions();

		this.scrollContainer
			.querySelectorAll(".heading-indicator")
			.forEach((marker) => marker.remove());

		this.headers.forEach((header) => {
			const headerOffset = header.offsetTop - this.article.offsetTop;
			const position = (headerOffset / articleHeight) * 100;
			const marker = this.createHeaderMarker(header, position, isHorizontal);
			this.scrollContainer.appendChild(marker);
		});
	}

	update() {
		const isHorizontal = window.innerWidth <= this.breakpoint;

		if (this.currentLayout !== isHorizontal) {
			this.currentLayout = isHorizontal;
			this.resetIndicatorStyles();
			this.updateHeaderMarkers(isHorizontal);
		}

		this.updateIndicator(isHorizontal);
	}
}

const scrollIndicator = new ScrollProgressIndicator();

              
            
!
999px

Console