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 class="scroller" data-scroller>
	<header data-header>
		<nav class="header__nav">
			<div class="header__left-content">
				<a class="header__home-link" href="#home">Home</a>
			</div>
			<ul class="header__list">
				<li>
					<a href="#about-us" data-link>About us</a>
				</li>
				<li>
					<a href="#the-flavours" data-link>The flavours</a>
				</li>
				<li>
					<a href="#get-in-touch" data-link>Get in touch</a>
				</li>
			</ul>
		</nav>
	</header>
	<main>
		<section data-section="raspberry" id="home">
			<div class="container grid">
				<div class="section__img-col">
					<img class="section__img" src="https://assets.codepen.io/85648/ice-creams-03.svg" alt="drawing of three smiling ice cream scoops in a bowl" />
				</div>
				<div class="section__content-col">
					<h1>Happy Face Ice Cream Parlour</h1>
					<p>A little ice cream parlour with big dreams.</p>
					<p>We pride ourselves on flavours beyond your wildest dreams, and customer service that’s out of this world!</p>
					<p>Order your favourite flavours online, or call us today to find out how we can make your special occasions extra delightful!</p>
					<a class="section__link" href="#0">Shop now</a>
				</div>
			</div>
		</section>
		<section data-section="mint" id="about-us">
			<div class="container grid">
				<div class="section__img-col">
					<img src="https://assets.codepen.io/85648/ice-creams-04.svg" alt="smiling scoop of ice cream with drips" />
				</div>
				<div class="section__content-col">
					<h2>About us</h2>
					<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
				</div>
			</div>
		</section>
		<section data-section="vanilla" id="the-flavours">
			<div class="container grid">
				<div class="section__img-col">
					<img src="https://assets.codepen.io/85648/ice-creams-01.svg" alt="three happy ice creams" />
				</div>
				<div class="section__content-col">
					<h2>The flavours</h2>
					<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
				</div>
			</div>
		</section>
		<section data-section="chocolate" id="get-in-touch">
			<div class="container grid">
				<div class="section__img-col">
					<img class="section__img" src="https://assets.codepen.io/85648/ice-creams-02.svg" alt="spoon with drips" />
				</div>
				<div class="section__content-col">
					<h2 data-section-title>Get in touch</h2>
					<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
				</div>
			</div>
		</section>
	</main>
</div>
              
            
!

CSS

              
                @import url("https://fonts.googleapis.com/css?family=Open+Sans:400,700");

/* Variables */
:root {
	--mint: #5ae8d5;
	--chocolate: #573e31;
	--raspberry: #f2308e;
	--vanilla: #faf2c8;
	
	--headerText: var(--vanilla);
	--headerBg: var(--raspberry);
}

/* --Reset and boilerplate stuff-- */
* {
	box-sizing: border-box;
}

body {
	font-family: "Open Sans", sans-serif;
	margin: 0;
	background: var(--vanilla);
	color: var(--chocolate);
	line-height: 1.6;
}

ul {
	list-style: none;
	margin: 0;
	padding: 0;
}

svg {
	width: 100%;
	height: auto;
	fill: currentColor;
}

img {
	display: block;
	max-width: 100%;
}

h1 {
	line-height: 1.2;
}

@media (min-width: 40em) {
	body {
		font-size: 1.2rem;
	}
	
	h1 {
		font-size: 3.5rem;
	}
}

a {
	color: inherit;
	text-decoration: none;
}

section {
	padding: 5rem 0;
	min-height: 100vh;
	display: grid;
	place-items: center;
}
/* ----- */

/* --Header-- */
header {
	position: fixed;
	width: 100%;
	padding: 0 max(1rem, 3vw);
	background-color: var(--headerBg);
	color: var(--headerText);
	font-weight: 700;
}
/* ----- */

/* --Layout stuff-- */
.section__img {
	max-width: 12rem;
	margin: 0 auto;
}

@media (min-width: 40em) {
	.section__img {
		max-width: none;
	}
}

section:last-child .section__img {
	max-width: 10rem;
}

.container {
	max-width: 60rem;
	padding: 0 min(2rem, 5vw);
}

@media (min-width: 40rem) {
	.grid {
		display: grid;
		align-items: center;
		grid-template-columns: 1fr 2fr;
		gap: 3rem;
	}
}

.header__nav {
	display: flex;
	align-items: center;
}

.header__list {
	display: none;
	margin-left: auto;
}

@media (min-width: 40rem) {
	.header__list {
		display: flex;
	}
}

.header__list a {
	display: block;
	padding: 1rem;
}
/* ----- */

/* --Secton specific styles, not related to demo-- */
.section__link {
	display: inline-block;
	background: var(--chocolate);
	padding: 1rem 1.5rem;
	color: var(--vanilla);
	border-radius: 0.5rem;
	font-weight: 700;
	margin-top: 1.5rem;
}
/* ----- */

/* --Section colors-- */
[data-section="raspberry"] {
	background-color: var(--raspberry);
	color: var(--vanilla);
}

[data-section="mint"]  {
	background-color: var(--mint);
	color: var(--chocolate);
}

[data-section="vanilla"] {
	background-color: var(--vanilla);
	color: var(--chocolate);
}

[data-section="chocolate"] {
	background-color: var(--chocolate);
	color: var(--vanilla);
}
/* ----- */

/* --Setting the custom properties for header colors-- */
[data-theme="raspberry"]  {
	--headerText: var(--raspberry);
	--headerBg: var(--vanilla);
}

[data-theme="mint"] {
	--headerText: var(--mint);
	--headerBg: var(--chocolate);
}

[data-theme="chocolate"]  {
	--headerText: var(--chocolate);
	--headerBg: var(--vanilla);
}
/* ----- */

/* --Setting the scroll root - because Codepen puts the content in an iframe-- */
.scroller {
	height: 100vh;
	width: 100%;
	overflow: auto;
}
/* ----- */
              
            
!

JS

              
                const header = document.querySelector('[data-header]')
const sections = [...document.querySelectorAll('[data-section]')]
const scrollRoot = document.querySelector('[data-scroller]')
const headerLinks = [...document.querySelectorAll('[data-link]')]

let prevYPosition = 0
let direction = 'up'

const options = {
	root: scrollRoot,
	rootMargin: `${header.offsetHeight * -1}px`,
	threshold: 0
}

const getTargetSection = (entry) => {
	const index = sections.findIndex((section) => section == entry.target)
	
	if (index >= sections.length - 1) {
	 return entry.target
	} else {
	 return sections[index + 1]
	}
}

const updateColors = (target) => {
	const theme = target.dataset.section
	header.setAttribute('data-theme', theme)
}

const shouldUpdate = (entry) => {
	if (direction === 'down' && !entry.isIntersecting) {
		return true
	}

	if (direction === 'up' && entry.isIntersecting) {
		return true
	}
	
	return false
}

const onIntersect = (entries, observer) => {
	entries.forEach((entry) => {
		if (scrollRoot.scrollTop > prevYPosition) {
			direction = 'down'
		} else {
			direction = 'up'
		}
		
		prevYPosition = scrollRoot.scrollTop
		
		const target = direction === 'down' ? getTargetSection(entry) : entry.target
		
		if (shouldUpdate(entry)) {
			updateColors(target)
		}
	})
}

const observer = new IntersectionObserver(onIntersect, options)

sections.forEach((section) => {
	observer.observe(section)
})

              
            
!
999px

Console