<header>
	I'm a header, beep boop.
</header>

<main>
	<h1>Article title yay \o/</h1>
<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>

	<p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>
	
	<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>

	<p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>

<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>

	<p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>
	
	<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>

	<p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>
	
<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>

	<p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>
	
	<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>

	<p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>
	
</main>

<aside>
	<h2>Subscribe now!</h2>
	<input type="email"/>
	<button>Subscribe</button>
</aside>
// The Timed newsletter popup will only appear if the user has scrolled down at least 50 percent and has been on the page for 30 seconds

// Could add the [data-site-visit-duration] attribute to make sure the user has been on the site for a while

// Timed newsletter popup
html[data-vertical-scroll-progress~='50'][data-page-visit-duration~="30"] aside {
	position: fixed;
	width:18em;
	height:8em;
	max-height:80vh;
	padding:2em;
	left:calc(50% - 9em);
	top:calc(50% - 5em);
	background:#fff;
	box-shadow:0 .25em 1em rgba(0,0,0,.5);
}

aside {
	padding:0 1em 2em;
}

// Floating header logic
header {
	width: 100%;
  min-height: 3em;
  background-color: #eee;
}

html:not([data-vertical-scroll='0']) {
	
	body {
	  padding-top: 3em;
	}
	
	header {
		position: fixed;
		top: 0;
		background-color: #fff;
		box-shadow: 0 0 .5em rgba(0, 0, 0, .5);
	}
	
}


// Cosmetics
header {
	box-sizing: border-box;
	padding: 1em;
	transition: background-color .25s;
}

main {
	padding:0 1em;
}
View Compiled
// Clear console so we our console is clutter free before we start
console.clear();


// The debounce function receives our function as a parameter
const debounce = (fn) => {

  // This holds the requestAnimationFrame reference, so we can cancel it if we wish
  let frame;

  // The debounce function returns a new function that can receive a variable number of arguments
  return (...params) => {
    
    // If the frame variable has been defined, clear it now, and queue for next frame
    if (frame) { 
      cancelAnimationFrame(frame);
    }

    // Queue our function call for the next frame
    frame = requestAnimationFrame(() => {
      
      // Call our function and pass any params we received
      fn(...params);
    });

  } 
};

// Returns an array of steps from value to 0
const stack = (value, step) => {
	value = Math.floor(value / step) * step;
	const parts = [value];
	while (value > 0) {
		value -= step;
		parts.push(value);
	}
	return parts.reverse();
}

// Creates an environment store at a certain element
const createEnvStore = (props, root = document.documentElement) => {
	const data = root.dataset;
	const sync = () => {
		Object.entries(props).forEach(([key, value]) => {
			data[key] = Array.isArray(value) ? value.join(' ') : value;
		})
	}
	sync();
	return (key, value) => {
		props[key] = value;
		sync();
		console.log(data);
	}
};

// Set default environment parameters
const updateEnv = createEnvStore({
	'verticalScroll': window.scrollY,
	'verticalScrollProgress': 0,
	'pageVisitDuration': 0,
	'siteVisitDuration': 0
});

// Listen for new scroll events, here we debounce our scroll handler function for performance reasons
document.addEventListener('scroll', debounce(() => {
	
	const scrollOffset = window.scrollY;
	const pageHeight = document.documentElement.scrollHeight - window.innerHeight;
	const scrollProgress = Math.min(1, scrollOffset / pageHeight);
	
	// Set current scroll offset (we use this to make the floating header)
	updateEnv('verticalScroll', scrollOffset);
	
	// Calculate current progress in percentages
	updateEnv('verticalScrollProgress', stack(Math.round(scrollProgress * 100), 5));
	
}), { passive: true });


// Determine How long has the user been on the page, updates the `pageVisitDuration` each 15 seconds, the siteVisitDuration is set in minutes.
const pageOpenDate = Date.now();

// Retrieves the site visit duration, or, if not defined, 0
const siteVisitDuration = parseInt(localStorage.getItem('siteVisitDuration') || 0, 10);

// Set current amount of visits
updateEnv('siteVisitDuration', stack(siteVisitDuration / 60, 1));

// Update each 15 seconds
setInterval(() => {
	// Page visit duration in seconds
	const pageVisitDuration = Math.round(Date.now() - pageOpenDate) / 1000;
	
	// Update `pageVisitDuration` with 15 second intervals (0 15 30 45 ...)
	updateEnv('pageVisitDuration', stack(pageVisitDuration, 15));
	
	// Update `siteVisitDuration` with 1 minute intervals (0 1 2 3 ...)
	updateEnv('siteVisitDuration', stack((siteVisitDuration + pageVisitDuration) / 60, 1));

	// Remember current duration
	localStorage.setItem('siteVisitDuration', siteVisitDuration + pageVisitDuration);
	
}, 15000);

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.