HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
If the stylesheet you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<header>
<div class=hero_bg></div>
<h1>Performant lazy-load method using the Intersection Observer API</h1>
</header>
<section>
<h2 class=centered-1>No visual jank, runs at the full 60fps</h2>
<!-- <img data-LL-src="https://source.unsplash.com/pFqrYbhIAXs/?w=1920x1080"> -->
<img data-LL-src="https://websemantics.uk/unsplash/needle-on-the-record.jpg">
<noscript>
<img src="https://websemantics.uk/unsplash/needle-on-the-record.jpg">
</noscript>
<a href="https://unsplash.com/@lukechesser" target=_blank rel="noopener noreferrer">
<svg><use xlink:href="#icon_camera"></use></svg>
Luke Chesser
</a>
</section>
<section>
<h2 class=centered-2>Ultra lightweight vanilla JavaScript</h2>
<!-- <img data-LL-src="https://source.unsplash.com/bq-1seg1cPI/?w=1920x1080"> -->
<img data-LL-src="https://websemantics.uk/unsplash/busy-bike.jpg">
<noscript>
<img src="https://websemantics.uk/unsplash/busy-bike.jpg">
</noscript>
<a href="https://unsplash.com/@clemono2" target=_blank rel="noopener noreferrer">
<svg><use xlink:href="#icon_camera"></use></svg>
Clem Onojeghuo
</a>
</section>
<section>
<h2 class=centered-3>Images loaded when JavaScript is unavailable</h2>
<!-- <img data-LL-src="https://source.unsplash.com/X2gM-SIufpU/?w=1920x2390"> -->
<img data-LL-src="https://websemantics.uk/unsplash/sunday-delight.jpg">
<noscript>
<img src="https://websemantics.uk/unsplash/sunday-delight.jpg">
</noscript>
<a href="https://unsplash.com/@foodess" target=_blank rel="noopener noreferrer">
<svg><use xlink:href="#icon_camera"></use></svg>
Jennifer Pallian
</a>
</section>
<section>
<h2 class=centered-4>Images loaded when IntersectionObserver is unsupported.</h2>
<!-- <img data-LL-src="https://source.unsplash.com/AYSorKGDm10/?w=1920x1080"> -->
<img data-LL-src="https://websemantics.uk/unsplash/red-door.jpg">
<noscript>
<img src="https://websemantics.uk/unsplash/red-door.jpg">
</noscript>
<a href="https://unsplash.com/@shttefan" target=_blank rel="noopener noreferrer">
<svg><use xlink:href="#icon_camera"></use></svg>
SHTTEFAN
</a>
</section>
<section>
<h2 class=centered>Neat huh?</h2>
<!-- <img data-LL-src="https://source.unsplash.com/PC_lbSSxCZE/?w=1920x1080"> -->
<img data-LL-src="https://websemantics.uk/unsplash/excalibur-sparkles.jpg">
<noscript>
<img src="https://websemantics.uk/unsplash/excalibur-sparkles.jpg">
</noscript>
<a href="https://unsplash.com/@krisroller" target=_blank rel="noopener noreferrer">
<svg><use xlink:href="#icon_camera"></use></svg>
Kristopher Roller
</a>
</section>
<footer>
<h2>Details</h2>
<p>Method utilises the new Intersection Observer API. Which has zero jank and runs at the full 60fps using ultra lightweight vanilla JavaScript.</p>
<p>Under fallback conditions (method unsupported, no JavaScript available) images are loaded as normal.</p>
<h2>Credits</h2>
<p>Based heavily on Dean Humes article: <a class=details_lnk title="[new window]" target=_blank href="http://www.deanhume.com/Home/BlogPost/lazy-loading-images-using-intersection-observer/10163">Lazy-loading images using Intersection Observer</a></p>
<p>Images from <a class=details_lnk target=_blank title="[new window]" href="https://unsplash.com/">Unsplash</a></p>
<h2>More info</h2>
<p>MDN: <a class=details_lnk target=_blank title="[new window]" href="https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API">Intersection Observer API</a></p>
<p>Google Developers: <a class=details_lnk target=_blank title="[new window]" href="https://developers.google.com/web/updates/2016/04/intersectionobserver">IntersectionObserver’s coming into view</a></p>
<p>Can I Use: <a class=details_lnk target=_blank title="[new window]" href="http://caniuse.com/#feat=intersectionobserver">browser support</a></p>
<p class=myStuff><a class=details_lnk target=_blank title="[new window]" href="https://codepen.io/2kool2/pens/popular/?grid_type=list#">Pens by Mike Foskett</a> — <a class=details_lnk target=_blank title="[new window]" href="https://websemantics.uk/">webSemantics</a></p>
</footer>
<svg class=visually-hidden>
<defs>
<symbol id="icon_camera" viewBox="0 0 32 32">
<path d="M20.8 18.1c0 2.7-2.2 4.8-4.8 4.8s-4.8-2.1-4.8-4.8c0-2.7 2.2-4.8 4.8-4.8 2.7.1 4.8 2.2 4.8 4.8zm11.2-7.4v14.9c0 2.3-1.9 4.3-4.3 4.3h-23.4c-2.4 0-4.3-1.9-4.3-4.3v-15c0-2.3 1.9-4.3 4.3-4.3h3.7l.8-2.3c.4-1.1 1.7-2 2.9-2h8.6c1.2 0 2.5.9 2.9 2l.8 2.4h3.7c2.4 0 4.3 1.9 4.3 4.3zm-8.6 7.5c0-4.1-3.3-7.5-7.5-7.5-4.1 0-7.5 3.4-7.5 7.5s3.3 7.5 7.5 7.5c4.2-.1 7.5-3.4 7.5-7.5z"></path>
</symbol>
</defs>
</svg>
@media (min-width: 20em) {
:root {
font-size: calc(1rem + ((1vw - 0.2em) * 1));
}
}
@media (min-width: 120em) {
:root {
font-size: 2em;
}
}
body {
font-family: sans-serif;
line-height: 1.5;
margin: 0;
text-align: center;
}
h1 {
color: #fff;
font-size: 1.5em;
font-weight: 100;
margin: 1rem;
padding: 1rem .5rem;
position: absolute; /* Collapses box */
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-shadow: 0 0 2px #000, 0 1px 1px #000;
background-color: rgba(0,0,0, .5);
/* backdrop-filter: grayscale(1) contrast(3) blur(5px); */
}
h2 {
margin: 2em 0 1em;
font-size: 1.25em;
font-weight: 100;
}
[class^="centered"] {
color: #fff;
background-color: rgba(0,0,0, .5);
position: absolute; /* Collapses box */
top: 50px;
left: 50%;
transform: translate(-50%, 0);
text-shadow: 0 0 2px #000, 0 1px 1px #000;
letter-spacing: 0.05em;
/* white-space: nowrap; */
z-index: 1;
margin:0;
padding: .25em .5em;
}
header,
footer,
section {
display: block;
position: relative;
min-height: calc(100vw / 16 * 10);
}
footer {
min-height: 0;
max-width: 36em;
margin: 0 auto;
padding: 0 1rem;
}
.myStuff, .myStuff .details_lnk {
background-color: #f7f7f7;
}
.hero_bg {
position: absolute;
width: 100%;
top: 0;
right: 0;
bottom: 0;
left;
background-image: url(https://websemantics.uk/unsplash/duvet-days.jpg);
background-size:cover;
/* filter: blur(1px); */
}
img {
display:block;
max-width: 100%;
margin:0 auto;
}
a {
position: absolute;
right: 0;
bottom: 0;
background-color: rgba(0,0,0,.5);
color: #fff;
text-decoration: none;
padding: .25em 1em;
}
.details_lnk {
position: relative;
padding: 0;
text-decoration: underline;
background-color: #fff;
color: #000;
}
svg {
width: .9em;
height: .9em;
vertical-align: text-top;
fill: #ccc;
margin-right:.35em;
}
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip: rect(1px, 1px, 1px, 1px);
}
.-js-fadeIn {
animation-name: fadeIn;
animation-duration: 2s;
animation-timing-function: cubic-bezier(0, 0, 0.4, 1);
animation-fill-mode: forwards;
}
@keyframes fadeIn {
from {opacity: 0;}
to {opacity: 1;}
}
// Dean Hume: https://deanhume.github.io/lazy-observer-load/
// TODO: background-images
var lazy_loader = (() => {
"use strict";
const dataAttr = "data-LL-src";
const imageLoaded = "-js-lazyImageLoaded";
const imageFadeClass = "-js-fadeIn";
// Get all of the images that are marked up to lazy load
const images = document.querySelectorAll("[" + dataAttr + "]");
const config = {
// If the image gets within 50px in the Y axis, start the download.
//rootMargin: '50px 0px',
rootMargin: '-50px 0px', // 50px into viewport before loading so you can see it kick in
threshold: 0.01
};
let imageCount = images.length;
let observer;
// If we don't have support for intersection observer, loads the images immediately
if (!('IntersectionObserver' in window)) {
console.log("No IntersectionObserver support");
Array.from(images).forEach(image => preloadImage(image));
} else {
// It is supported, load the images
console.log("IntersectionObserver supported");
observer = new IntersectionObserver(onIntersection, config);
images.forEach(image => {
if (image.classList.contains(imageLoaded)) {
return;
}
observer.observe(image);
});
}
// Fetch image for given URL
function fetchImage(url) {
return new Promise((resolve, reject) => {
const image = new Image();
image.src = url;
image.onload = resolve;
image.onerror = reject;
});
}
// Preloads the image
function preloadImage(image) {
const src = image.getAttribute(dataAttr);
if (!src) {
return;
}
return fetchImage(src).then(() => {
applyImage(image, src);
});
}
// Load all of the images immediately
function loadImagesImmediately(images) {
Array.from(images).forEach(image => preloadImage(image));
}
// Disconnect the observer
function disconnect() {
if (!observer) {
return;
}
observer.disconnect();
}
// On intersection
function onIntersection(entries) {
// Disconnect if we've already loaded all of the images
if (imageCount === 0) {
observer.disconnect();
}
// Loop through the entries
entries.forEach(entry => {
// Are we in viewport?
if (entry.intersectionRatio > 0) {
imageCount--;
// Stop watching and load the image
observer.unobserve(entry.target);
preloadImage(entry.target);
}
});
}
// Apply the image
function applyImage(img, src) {
// Prevent this from being lazy loaded a second time.
img.classList.add(imageLoaded);
img.src = src;
img.classList.add(imageFadeClass);
}
})();
Also see: Tab Triggers