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

              
                <main>
	<header>
		<h1>💡 Compare Slider Prototype</h1>
		<p>This slider uses CSS Scroll Snap and some vanilla JavaScript to create a scrollable sticky header and sticky section subheadings on mobile.</p>
	</header>
	
	<div id="compare-sticky" class="compare-sticky">
		<div class="compare-sticky__product">
			<img class="compare-sticky__image" src="https://placehold.it/200" alt="Product #1">
			<a class="compare-sticky__link" href="#">Learn More</a>
		</div>
		<div class="compare-sticky__product">
			<img class="compare-sticky__image" src="https://placehold.it/200" alt="Product #2">
			<a class="compare-sticky__link" href="#">Learn More</a>
		</div>
		<div class="compare-sticky__product">
			<img class="compare-sticky__image" src="https://placehold.it/200" alt="Product #3">
			<a class="compare-sticky__link" href="#">Learn More</a>
		</div>
		<div class="compare-sticky__product">
			<img class="compare-sticky__image" src="https://placehold.it/200" alt="Product #4">
			<a class="compare-sticky__link" href="#">Learn More</a>
		</div>
	</div>
	
	<ul id="compare" class="compare">
		<li class="compare__product">
			<h2 class="compare__product-heading">Product #1</h2>
			<img class="compare__product-image" src="https://placehold.it/200" alt="">
			<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
			<div class="u-text-align-center">
				<a class="compare__product-link" href="#">Learn More</a>
			</div>
			
			<h3 class="compare__product-subheading">
				<span class="compare__product-subheading-inner">Highlight #1</span>
			</h3>
			<ul>
				<li>Bullet</li>
				<li>Bullet</li>
				<li>Bullet</li>
			</ul>
			
			<h3 class="compare__product-subheading">
				<span class="compare__product-subheading-inner">Highlight #2</span>
			</h3>
			<p>Atque voluptates vitae necessitatibus doloribus nam voluptas cumque nobis architecto.</p>
			
			<h3 class="compare__product-subheading">
				<span class="compare__product-subheading-inner">Highlight #3</span>
			</h3>
			<p>Soluta libero aspernatur illo. Mollitia dolor animi sint doloremque excepturi quasi odio quis, nostrum expedita nobis dolorem! Earum eligendi autem eos aperiam?</p>
			
			<h3 class="compare__product-subheading">
				<span class="compare__product-subheading-inner">Highlight #4</span>
			</h3>
			<p class="placeholder">Placeholder</p>
		</li>
		
		<li class="compare__product">
			<h2 class="compare__product-heading">Product #2</h2>
			<img class="compare__product-image" src="https://placehold.it/200" alt="">
			<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
			<div class="u-text-align-center">
				<a class="compare__product-link" href="#">Learn More</a>
			</div>
			
			<h3 class="compare__product-subheading">
				<span class="compare__product-subheading-inner">Highlight #1</span>
			</h3>
			<ul>
				<li>Bullet</li>
				<li>Bullet</li>
				<li>Bullet</li>
			</ul>
			
			<h3 class="compare__product-subheading">
				<span class="compare__product-subheading-inner">Highlight #2</span>
			</h3>
			<p>Atque voluptates vitae necessitatibus doloribus nam voluptas cumque nobis architecto.</p>
			
			<h3 class="compare__product-subheading">
				<span class="compare__product-subheading-inner">Highlight #3</span>
			</h3>
			<p>Soluta libero aspernatur illo. Mollitia dolor animi sint doloremque excepturi quasi odio quis, nostrum expedita nobis dolorem! Earum eligendi autem eos aperiam?</p>
			
			<h3 class="compare__product-subheading">
				<span class="compare__product-subheading-inner">Highlight #4</span>
			</h3>
			<p class="placeholder">Placeholder</p>
		</li>
		
		<li class="compare__product">
			<h2 class="compare__product-heading">Product #3</h2>
			<img class="compare__product-image" src="https://placehold.it/200" alt="">
			<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
			<div class="u-text-align-center">
				<a class="compare__product-link" href="#">Learn More</a>
			</div>
			
			<h3 class="compare__product-subheading">
				<span class="compare__product-subheading-inner">Highlight #1</span>
			</h3>
			<ul>
				<li>Bullet</li>
				<li>Bullet</li>
				<li>Bullet</li>
			</ul>
			
			<h3 class="compare__product-subheading">
				<span class="compare__product-subheading-inner">Highlight #2</span>
			</h3>
			<p>Atque voluptates vitae necessitatibus doloribus nam voluptas cumque nobis architecto.</p>
			
			<h3 class="compare__product-subheading">
				<span class="compare__product-subheading-inner">Highlight #3</span>
			</h3>
			<p>Soluta libero aspernatur illo. Mollitia dolor animi sint doloremque excepturi quasi odio quis, nostrum expedita nobis dolorem! Earum eligendi autem eos aperiam?</p>
			
			<h3 class="compare__product-subheading">
				<span class="compare__product-subheading-inner">Highlight #4</span>
			</h3>
			<p class="placeholder">Placeholder</p>
		</li>
		
		<li class="compare__product">
			<h2 class="compare__product-heading">Product #4</h2>
			<img class="compare__product-image" src="https://placehold.it/200" alt="">
			<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
			<div class="u-text-align-center">
				<a class="compare__product-link" href="#">Learn More</a>
			</div>
			
			<h3 class="compare__product-subheading">
				<span class="compare__product-subheading-inner">Highlight #1</span>
			</h3>
			<ul>
				<li>Bullet</li>
				<li>Bullet</li>
				<li>Bullet</li>
			</ul>
			
			<h3 class="compare__product-subheading">
				<span class="compare__product-subheading-inner">Highlight #2</span>
			</h3>
			<p>Atque voluptates vitae necessitatibus doloribus nam voluptas cumque nobis architecto.</p>
			
			<h3 class="compare__product-subheading">
				<span class="compare__product-subheading-inner">Highlight #3</span>
			</h3>
			<p>Soluta libero aspernatur illo. Mollitia dolor animi sint doloremque excepturi quasi odio quis, nostrum expedita nobis dolorem! Earum eligendi autem eos aperiam?</p>
			
			<h3 class="compare__product-subheading">
				<span class="compare__product-subheading-inner">Highlight #4</span>
			</h3>
			<p class="placeholder">Placeholder</p>
		</li>
	</ul>
</main>

              
            
!

CSS

              
                /* ------------------------------ */
/* Base                           */
/* ------------------------------ */

html {
	box-sizing: border-box;
}

*,
::after,
::before {
	box-sizing: inherit;
}

body {
	margin: 1rem auto 3rem;
	line-height: 1.4;
	tab-size: 4;
}

main {
	display: block; /* Fix IE rendering main as inline */
	margin: 0 auto;
	max-width: 77rem;
	padding-left: 1rem;
	padding-right: 1rem;
}

header {
	max-width: 42rem;
	margin: auto;
}

.placeholder {
	height: 500px;
}

/* ------------------------------ */
/* Compare                        */
/* ------------------------------ */

.compare {
	display: flex;
	margin: 0;
	padding: 0;
	list-style: none;
}

.compare__product {
	flex: 1 1 0%;
	padding: 1rem;
}

.compare__product:nth-child(even) {
	background-color: #eee;
}

.compare__product-heading {
	text-align: center;
}

.compare__product-image {
	display: block;
	max-width: 100%;
	margin: auto;
}

.compare-sticky {
	position: fixed;
	top: 0;
	left: 0;
	z-index: 1;
	display: flex;
	align-items: stretch;
	width: 100%;
	height: 70px;
	background: #fff;
	box-shadow:
		0 3px 6px rgba(0, 0, 0, 0.16);
	transition:
		opacity 300ms ease,
		visibility 300ms linear 0ms;
	opacity: 0;
	visibility: hidden;
	pointer-events: none;
	user-select: none;
	-ms-overflow-style: none;
	overflow: -moz-scrollbars-none;
}

.compare-sticky::-webkit-scrollbar {
	display: none;
}

.compare-sticky.is-active {
	opacity: 1;
	visibility: visible;
	pointer-events: auto;
	user-select: auto;
}

.compare-sticky__product {
	display: flex;
	align-items: center;
	flex: 1 1 0%;
}


.compare-sticky__image {
	width: 70px;
	margin-right: 1rem;
}

.compare-sticky__link {
	flex-grow: 1;
	text-align: center;
}

/* Mobile */
@media (max-width: 767px) {
	.compare-sticky,
	.compare {
		width: 100vw; /* https://codepen.io/jakob-e/pen/YbrRYN */
		margin-right: calc(50% - 50vw);
		margin-left: calc(50% - 50vw);
		scroll-snap-type: x mandatory;
		overflow: scroll;
		-webkit-overflow-scrolling: touch;
	}

	/* 💡 Disable scroll snap while matching scroll position */
	/* Chrome & Android browsers _immediately_ snap to the destination position when you programatically update `el.scrollLeft` */
	.snaps-immediately .compare-sticky.is-syncing,
	.snaps-immediately .compare.is-syncing {
		scroll-snap-type: none;
	}
	
	.compare-sticky__product,
	.compare__product {
		min-width: 50%;
		scroll-snap-align: start;
	}
	
	.compare__product-subheading {
		height: 1rem;
		white-space: nowrap;
		text-align: center;
	}
	
	.compare__product:last-child .compare__product-subheading-inner {
		display: block;
		position: absolute;
		width: 100vw;
		left: 0;
		background: lightblue;
	}
}

/* Desktop */
@media (min-width: 768px) {
	.compare {
		scroll-snap-type: x mandatory;
	}
}

/* ------------------------------ */
/* Utilities                      */
/* ------------------------------ */

.u-text-align-center {
	text-align: center;
}

              
            
!

JS

              
                (function () {
	/**
	 * Check whether this browser immediately snaps elements
	 * when you programmatically update `el.scrollLeft`.
	 * @type {Boolean}
	 */
	var snapsImmediately = (function () {
		// Build test slider
		var test = document.createElement('div');
		test.className = 'test-snap colors';
		test.innerHTML = '<div>1</div><div>2</div><div>3</div><div>4</div><style>.test-snap{display:-webkit-box;display:-ms-flexbox;display:flex;position:fixed;bottom:0;left:0;width:100vw;overflow:scroll;-ms-scroll-snap-type:x mandatory;scroll-snap-type:x mandatory;-webkit-overflow-scrolling:touch;opacity:0}.test-snap>*{-webkit-box-flex:1;-ms-flex:1 1 0%;flex:1 1 0%;min-width:50vw;height:1rem;scroll-snap-align:start}</style>';

		// Add test element to the page
		document.body.appendChild(test);

		// Test scroll position
		var scrollPosition = Math.ceil(window.innerWidth * 0.75); // Round value because `scrollLeft` only accepts whole numbers
		test.scrollLeft = scrollPosition;
		var snapsImmediately = test.scrollLeft !== scrollPosition;

		// Add class to document root
		if (snapsImmediately) {
			document.documentElement.className += ' snaps-immediately';
		}

		// Remove test element
		test.parentNode.removeChild(test);

		return snapsImmediately;
	})();

	/**
	 * QSA = "Query Selector All" that returns an Array instead of a NodeList.
	 * @param  {String}   selector
	 * @param  {Element}  context
	 * @return {Array}
	 */
	function qsa(selector, context) {
		return selector ? Array.prototype.slice.call((context || document).querySelectorAll(selector)) : [];
	}

	/**
	 * Run a function once the DOM is ready.
	 * @param  {Function} fn
	 * @return {void}
	 */
	function ready(fn) {
		if (document.readyState !== 'loading') {
			fn();
		} else {
			document.addEventListener('DOMContentLoaded', fn);
		}
	}
	
	/**
	 * Returns a function, that, as long as it continues to be invoked, will not
	 * be triggered. The function will be called after it stops being called for
	 * N milliseconds. If `immediate` is passed, trigger the function on the
	 * leading edge, instead of the trailing.
	 * @see https://davidwalsh.name/javascript-debounce-function
	 * @param  {Function}  func
	 * @param  {Number}    wait
	 * @param  {Boolean}   immediate
	 * @return {Function}
	 */
	function debounce(func, wait, immediate) {
		var timeout;
		return function() {
			var context = this, args = arguments;
			var later = function() {
				timeout = null;
				if (!immediate) func.apply(context, args);
			};
			var callNow = immediate && !timeout;
			clearTimeout(timeout);
			timeout = setTimeout(later, wait);
			if (callNow) func.apply(context, args);
		};
	}
	
	/**
	 * Throttle functions by a given number of milliseconds.
	 * @see https://remysharp.com/2010/07/21/throttling-function-calls
	 * @param  {Function}  fn
	 * @param  {Number}    threshhold
	 * @param  {Function}  scope
	 * @return {Function}
	 */
	function throttle(fn, threshhold, scope) {
		threshhold || (threshhold = 250);
		var last,
			deferTimer;
		return function () {
			var context = scope || this;

			var now = +new Date,
				args = arguments;
			if (last && now < last + threshhold) {
				// hold on to it
				clearTimeout(deferTimer);
				deferTimer = setTimeout(function () {
					last = now;
					fn.apply(context, args);
				}, threshhold);
			} else {
				last = now;
				fn.apply(context, args);
			}
		};
	}

	// Passive Event Listeners.
	// https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md#feature-detection
	var passive = false;
	try {
		var opts = Object.defineProperty({}, 'passive', {
			get: function get() { passive = { passive: true }; }
		});
		window.addEventListener('testPassive', null, opts);
		window.removeEventListener('testPassive', null, opts);
	} catch (error) {}

	// Elements
	var compare = document.querySelector('#compare');
	var compareSticky = document.querySelector('#compare-sticky');
	
	// Sticky
	function toggleSticky(event) {
		var firstLink = compare.querySelector('.compare__product-link');
		var firstLinkTop = firstLink.getBoundingClientRect().top;
		var isActive = compareSticky.classList.contains('is-active');

		// Show sticky when we scroll past the product links
		if (!isActive && firstLinkTop < 0) {
			compareSticky.classList.add('is-active');
		// Otherwise, hide sticky when we scroll back above the links
		} else if (isActive && firstLinkTop > 0) {
			compareSticky.classList.remove('is-active');
		}
	}
	
	ready(toggleSticky);
	window.addEventListener('scroll', throttle(toggleSticky, 100), passive);
	
	var scrollTarget = compare;
	var syncTarget = compareSticky;
	
	function matchSticky() {
		// If someone explicitly scrolled sticky, match compare to sticky
		if (scrollTarget === compare) {
			compareSticky.scrollLeft = compare.scrollLeft;
		// Otherwise, match sticky to compare
		} else {
			compare.scrollLeft = compareSticky.scrollLeft;
		}
	}
	
	// Start sync on *first* scroll event
	var startSync = debounce(function (event) {
		// Get which element we're scrolling
		scrollTarget = event.target;
		// Get which element we need to sync
		syncTarget = scrollTarget === compare ? compareSticky : compare;
		// Disable scroll snap on syncTarget to avoid premature snapping in Chrome & Android
		syncTarget.classList.add('is-syncing');
	}, 20, true);
	
	// End sync after *last* scroll event
	var endSync = debounce(function () {
		// Re-enable scroll snap
		syncTarget.classList.remove('is-syncing');
	}, 20, false);
	
	ready(matchSticky);
	compare.addEventListener('scroll', matchSticky, passive);
	compare.addEventListener('scroll', startSync, passive);
	compare.addEventListener('scroll', endSync, passive);
	compareSticky.addEventListener('scroll', matchSticky, passive);
	compareSticky.addEventListener('scroll', startSync, passive);
	compareSticky.addEventListener('scroll', endSync, passive);
	
	// HACK: Trigger a scroll to re-snap (in case shrinking the browser causes subheadings to overflow)
	window.addEventListener('resize', function () {
		compare.scrollLeft = compare.scrollLeft;
	}, passive);

})();

              
            
!
999px

Console