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 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.
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.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and 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 esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM 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.
<style>
/*!
* @see {@link https://cdnjs.cloudflare.com/ajax/libs/github-fork-ribbon-css/0.2.2/gh-fork-ribbon.css}
*/
.github-fork-ribbon{width:12.1em;height:12.1em;position:absolute;overflow:hidden;top:0;right:0;z-index:9999;pointer-events:none;font-size:13px;text-decoration:none;text-indent:-999999px;}.github-fork-ribbon.fixed{position:fixed;}.github-fork-ribbon:before,.github-fork-ribbon:after{position:absolute;display:block;width:15.38em;height:1.54em;top:3.23em;right:-3.23em;-webkit-box-sizing:content-box;box-sizing:content-box;-webkit-transform:rotate(45deg);-ms-transform:rotate(45deg);transform:rotate(45deg);}.github-fork-ribbon:before{content:"";padding:.38em 0;background-color:#a00;background-image:-webkit-gradient(linear,left top,left bottom,from(rgba(0,0,0,0)),to(rgba(0,0,0,0.15)));background-image:-webkit-linear-gradient(top,rgba(0,0,0,0),rgba(0,0,0,0.15));background-image:-o-linear-gradient(top,rgba(0,0,0,0),rgba(0,0,0,0.15));background-image:linear-gradient(to bottom,rgba(0,0,0,0),rgba(0,0,0,0.15));-webkit-box-shadow:0 .15em .23em 0 rgba(0,0,0,0.5);box-shadow:0 .15em .23em 0 rgba(0,0,0,0.5);pointer-events:auto;}.github-fork-ribbon:after{content:attr(data-ribbon);color:#fff;font:700 1em "Helvetica Neue",Helvetica,Arial,sans-serif;line-height:1.54em;text-decoration:none;text-shadow:0 -.08em rgba(0,0,0,0.5);text-align:center;text-indent:0;padding:.15em 0;margin:.15em 0;border-width:.08em 0;border-style:dotted;border-color:#fff;border-color:rgba(255,255,255,0.7);}.github-fork-ribbon.left-top,.github-fork-ribbon.left-bottom{right:auto;left:0;}.github-fork-ribbon.left-bottom,.github-fork-ribbon.right-bottom{top:auto;bottom:0;}.github-fork-ribbon.left-top:before,.github-fork-ribbon.left-top:after,.github-fork-ribbon.left-bottom:before,.github-fork-ribbon.left-bottom:after{right:auto;left:-3.23em;}.github-fork-ribbon.left-bottom:before,.github-fork-ribbon.left-bottom:after,.github-fork-ribbon.right-bottom:before,.github-fork-ribbon.right-bottom:after{top:auto;bottom:3.23em;}.github-fork-ribbon.left-top:before,.github-fork-ribbon.left-top:after,.github-fork-ribbon.right-bottom:before,.github-fork-ribbon.right-bottom:after{-webkit-transform:rotate(-45deg);-ms-transform:rotate(-45deg);transform:rotate(-45deg);}
</style>
</head>
<body>
<h1 class="title">img-lightbox Demo</h1>
<p>Responsive no-jQuery pure JS/CSS Lightbox for images, no dependencies, 10kb unminified source code, with demo</p>
<h2>Features</h2>
<ul>
<li>Simple initialization</li>
<li>SPA / PWA friendly: prevents multiple same events assigning</li>
<li>Debounced launch, default 500ms, custom rate can be set with rate property of options object</li>
<li>Preloading spinner that is unset after onload event succeeds</li>
<li>Pure CSS Retina Ready UI images, no external ones (prompted by github.com/jasomdotnet, thanks)</li>
<li>Custom event callbacks</li>
</ul>
<h2>CDN</h2>
<h4>jsDelivr</h4>
<p>
<code>https://cdn.jsdelivr.net/gh/englishextra/img-lightbox@latest/img-lightbox.min.js</code>
<br />
<code>https://cdn.jsdelivr.net/gh/englishextra/img-lightbox@latest/img-lightbox.min.css</code>
</p>
<h4>unpkg</h4>
<p>
<code>https://unpkg.com/img-lightbox@latest/img-lightbox.js</code>
<br />
<code>https://unpkg.com/img-lightbox@latest/img-lightbox.css</code>
</p>
<h2>Install</h2>
<h4>npm</h4>
<p>
<code>npm install img-lightbox</code>
</p>
<h4>bower</h4>
<p>
<code>bower install img-lightbox</code>
</p>
<h2>Setup</h2>
<p><code>class</code> is not required. They are used here to select elements. You may use some other method for elements selection.</p>
<p><code>data-src</code> is deprecated, but supported for compatibility.</p>
<p><code>href</code> is required, and contains URL of your image.</p>
<h2><a href="https://farm1.staticflickr.com/955/27854475488_5f82a379ca_z.jpg" class="img-lightbox-link" aria-hidden="true" rel="lightbox"><img src="https://farm1.staticflickr.com/955/27854475488_5f82a379ca_z.jpg" alt="Image Lightbox" /></a></h2>
<pre><code><a href="https://farm1.staticflickr.com/955/27854475488_5f82a379ca_z.jpg"
class="img-lightbox-link"
aria-hidden="true"
rel="lightbox"><img src="https://farm1.staticflickr.com/955/27854475488_5f82a379ca_z.jpg" alt="Image Lightbox" /></a></code></pre>
<h2>Initialize</h2>
<pre><code>imgLightbox("img-lightbox-link");</code></pre>
<h2>Tips</h2>
<p>SPA / PWA developers don't need to bother: built-in class is added to a link.</p>
<p>That way you avoid multiple assignments to a single element.</p>
<h2>Examples of event handling</h2>
<pre><code>(function(root){
"use strict";
if (root.imgLightbox) {
imgLightbox("img-lightbox-link", {
onCreated: function () {
/* show your preloader */
},
onLoaded: function () {
/* hide your preloader */
},
onError: function () {
/* hide your preloader */
},
onClosed: function () {
/* hide your preloader */
},
rate: 500 /* default: 500 */
});
}
})("undefined" !== typeof window ? window : this);</code></pre>
<h2>GitHub</h2>
<p><a href="https://github.com/englishextra/img-lightbox">englishextra/img-lightbox</a></p>
<h2>License</h2>
<p>Available under <a href="https://opensource.org/licenses/MIT">MIT License</a></p>
<a class="github-fork-ribbon right-top" href="https://github.com/englishextra/img-lightbox" target="_blank" rel="noopener noreferrer" title="Fork me on GitHub" data-ribbon="Fork me on GitHub">Fork me on GitHub</a>
/*!
* some animations from animate.css
* @see {@link https://github.com/daneden/animate.css}
* daneden.github.io/animate.css/
*/
/* .animated {
animation-duration: 2s;
animation-fill-mode: both;
} */
@-webkit-keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
.fadeIn {
-webkit-animation-name: fadeIn;
animation-name: fadeIn;
}
@-webkit-keyframes fadeInUp {
0% {
opacity: 0;
-webkit-transform: translate3d(0, 100%, 0);
transform: translate3d(0, 100%, 0);
}
100% {
opacity: 1;
-webkit-transform: none;
transform: none;
}
}
@keyframes fadeInUp {
0% {
opacity: 0;
-webkit-transform: translate3d(0, 100%, 0);
transform: translate3d(0, 100%, 0);
}
100% {
opacity: 1;
-webkit-transform: none;
transform: none;
}
}
.fadeInUp {
-webkit-animation-name: fadeInUp;
animation-name: fadeInUp;
}
@-webkit-keyframes fadeOut {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
@keyframes fadeOut {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
.fadeOut {
-webkit-animation-name: fadeOut;
animation-name: fadeOut;
}
@-webkit-keyframes fadeOutDown {
0% {
opacity: 1;
}
100% {
opacity: 0;
-webkit-transform: translate3d(0, 100%, 0);
transform: translate3d(0, 100%, 0);
}
}
@keyframes fadeOutDown {
0% {
opacity: 1;
}
100% {
opacity: 0;
-webkit-transform: translate3d(0, 100%, 0);
transform: translate3d(0, 100%, 0);
}
}
.fadeOutDown {
-webkit-animation-name: fadeOutDown;
animation-name: fadeOutDown;
}
/*!
* img lightbox
*/
.img-lightbox {
display: none;
opacity: 0;
width: 100%;
height: 100%;
position: fixed;
top: 0;
left: 0;
background-color: rgba(0, 0, 0, 0.92);
z-index: 999999;
}
.img-lightbox img {
display: block;
opacity: 0;
max-width: 90%;
max-height: 90%;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: transparent;
border: 0;
-webkit-box-shadow: 0.267rem 0.267rem 0.267rem 0 rgba(3, 3, 3, 0.3);
box-shadow: 0.267rem 0.267rem 0.267rem 0 rgba(3, 3, 3, 0.3);
vertical-align: middle;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
cursor: -webkit-zoom-out;
cursor: zoom-out;
margin: auto;
}
/*!
* reset for animate.css
* daneden.github.io/animate.css/
*/
.img-lightbox.animated,
.img-lightbox img.animated {
-webkit-animation-duration: 0.4s;
animation-duration: 0.4s;
-webkit-animation-fill-mode: both;
animation-fill-mode: both;
}
/*!
* another pure css spinner
* @see {@link https://epic-spinners.epicmax.co/}
*/
.img-lightbox .half-circle-spinner,
.img-lightbox .half-circle-spinner * {
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
.img-lightbox .half-circle-spinner {
width: 60px;
height: 60px;
border-radius: 100%;
position: relative;
margin: 0;
position: fixed;
top: 50%;
left: 50%;
margin-right: -50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
.img-lightbox .half-circle-spinner .circle {
content: "";
position: absolute;
width: 100%;
height: 100%;
border-radius: 100%;
border: calc(60px / 10) solid transparent;
}
.img-lightbox .half-circle-spinner .circle.circle-1 {
border-top-color: #ffffff;
-webkit-animation: half-circle-spinner-animation 1s infinite;
animation: half-circle-spinner-animation 1s infinite;
}
.img-lightbox .half-circle-spinner .circle.circle-2 {
border-bottom-color: #ffffff;
-webkit-animation: half-circle-spinner-animation 1s infinite alternate;
animation: half-circle-spinner-animation 1s infinite alternate;
}
@-webkit-keyframes half-circle-spinner-animation {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
@keyframes half-circle-spinner-animation {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
.img-lightbox.is-loaded .half-circle-spinner {
display: none;
}
.img-lightbox .btn-close {
position: fixed;
top: 1.000rem;
right: 1.000rem;
width: 1.500rem;
height: 1.500rem;
}
/*!
* pure css version
* @see {@link https://codepen.io/brissmyr/pen/egidw}
*/
.img-lightbox .btn-close:before,
.img-lightbox .btn-close:after {
content: " ";
width: 0.125em;
height: 1.5em;
position: absolute;
right: 0.625em;
background-color: #ffffff;
}
.img-lightbox .btn-close:before {
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
.img-lightbox .btn-close:after {
-webkit-transform: rotate(-45deg);
transform: rotate(-45deg);
}
/*!
* @see {@link https://github.com/englishextra/iframe-lightbox/issues/12}
*/
.img-lightbox--open {
overflow: hidden;
-webkit-overflow-scrolling: touch;
-ms-touch-action: auto;
touch-action: auto;
height: auto;
}
/*!
* @see {@link https://github.com/englishextra/img-lightbox}
* imgLightbox
* requires this very img-lightbox.js, and animate.css, img-lightbox.css
* @params {String} linkClass
* @params {Object} settings object
* imgLightbox(linkClass, settings)
* passes jshint
*/
/*jshint -W014 */
(function (root, document) {
"use strict";
var docElem = document.documentElement || "";
var docBody = document.body || "";
var animatedClass = "animated";
var appendChild = "appendChild";
var classList = "classList";
var createElement = "createElement";
var getAttribute = "getAttribute";
var getElementsByClassName = "getElementsByClassName";
var getElementsByTagName = "getElementsByTagName";
var innerHTML = "innerHTML";
var style = "style";
var _addEventListener = "addEventListener";
var _length = "length";
var btnCloseClass = "btn-close";
var containerClass = "img-lightbox";
var fadeInClass = "fadeIn";
var fadeInUpClass = "fadeInUp";
var fadeOutClass = "fadeOut";
var fadeOutDownClass = "fadeOutDown";
var imgLightboxOpenClass = "img-lightbox--open";
var imgLightboxLinkIsBindedClass = "img-lightbox-link--is-binded";
var isLoadedClass = "is-loaded";
var dummySrc = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=";
var isMobile = navigator.userAgent.match(/(iPad)|(iPhone)|(iPod)|(Android)|(PlayBook)|(BB10)|(BlackBerry)|(Opera Mini)|(IEMobile)|(webOS)|(MeeGo)/i);
var isTouch = isMobile !== null || document.createTouch !== undefined || "ontouchstart" in root || "onmsgesturechange" in root || navigator.msMaxTouchPoints;
var debounce = function (func, wait) {
var timeout;
var args;
var context;
var timestamp;
return function () {
context = this;
args = [].slice.call(arguments, 0);
timestamp = new Date();
var later = function () {
var last = new Date() - timestamp;
if (last < wait) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
func.apply(context, args);
}
};
if (!timeout) {
timeout = setTimeout(later, wait);
}
};
};
var callCallback = function (func, data) {
if (typeof func !== "function") {
return;
}
var caller = func.bind(this);
caller(data);
};
var hideImgLightbox = function (callback) {
var container = document[getElementsByClassName](containerClass)[0] || "";
var img = container ? container[getElementsByTagName]("img")[0] || "" : "";
var hideContainer = function () {
container[classList].remove(fadeInClass);
container[classList].add(fadeOutClass);
var hideImg = function () {
container[classList].remove(animatedClass);
container[classList].remove(fadeOutClass);
img[classList].remove(animatedClass);
img[classList].remove(fadeOutDownClass);
img.onload = function () {
container[classList].remove(isLoadedClass);
};
img.src = dummySrc;
container[style].display = "none";
callCallback(callback, root);
};
var timer = setTimeout(function () {
clearTimeout(timer);
timer = null;
hideImg();
}, 400);
};
if (container && img) {
img[classList].remove(fadeInUpClass);
img[classList].add(fadeOutDownClass);
var timer = setTimeout(function () {
clearTimeout(timer);
timer = null;
hideContainer();
}, 400);
}
docElem[classList].remove(imgLightboxOpenClass);
docBody[classList].remove(imgLightboxOpenClass);
};
var imgLightbox = function (linkClass, settings) {
var _linkClass = linkClass || "";
var options = settings || {};
var rate = options.rate || 500;
var touch = options.touch;
var onError = options.onError;
var onLoaded = options.onLoaded;
var onCreated = options.onCreated;
var onClosed = options.onClosed;
var link = document[getElementsByClassName](_linkClass) || "";
var container = document[getElementsByClassName](containerClass)[0] || "";
var img = container ? container[getElementsByTagName]("img")[0] || "" : "";
if (!container) {
container = document[createElement]("div");
container[classList].add(containerClass);
var html = [];
html.push('<img src="' + dummySrc + '" alt="" />');
html.push('<div class="half-circle-spinner"><div class="circle circle-1"></div><div class="circle circle-2"></div></div>');
html.push('<a href="javascript:void(0);" class="btn-close"></a>');
container[innerHTML] = html.join("");
docBody[appendChild](container);
img = container ? container[getElementsByTagName]("img")[0] || "" : "";
var btnClose = container ? container[getElementsByClassName](btnCloseClass)[0] || "" : "";
var handleImgLightboxContainer = function () {
hideImgLightbox(onClosed);
};
container[_addEventListener]("click", handleImgLightboxContainer);
btnClose[_addEventListener]("click", handleImgLightboxContainer);
root[_addEventListener]("keyup", function (ev) {
if (27 === (ev.which || ev.keyCode)) {
hideImgLightbox(onClosed);
}
});
}
var arrange = function (e) {
var hrefString = e[getAttribute]("href") || e[getAttribute]("data-src") || "";
var dataTouch = e[getAttribute]("data-touch") || "";
if (!hrefString) {
return;
}
var handleImgLightboxLink = function (ev) {
ev.stopPropagation();
ev.preventDefault();
docElem[classList].add(imgLightboxOpenClass);
docBody[classList].add(imgLightboxOpenClass);
container[classList].remove(isLoadedClass);
var logic = function logic() {
if (onCreated) {
callCallback(onCreated, root);
}
container[classList].add(animatedClass);
container[classList].add(fadeInClass);
img[classList].add(animatedClass);
img[classList].add(fadeInUpClass);
img.onload = function () {
container[classList].add(isLoadedClass);
if (onLoaded) {
callCallback(onLoaded, root);
}
};
img.onerror = function () {
if (onError) {
callCallback(onError, root);
}
};
img.src = hrefString;
container[style].display = "block";
};
debounce(logic, rate).call();
};
if (!e[classList].contains(imgLightboxLinkIsBindedClass)) {
e[classList].add(imgLightboxLinkIsBindedClass);
e[_addEventListener]("click", handleImgLightboxLink);
if (isTouch && (touch || dataTouch)) {
e[_addEventListener]("touchstart", handleImgLightboxLink);
}
}
};
if (container && img && link) {
var i,
l;
for (i = 0, l = link[_length]; i < l; i += 1) {
arrange(link[i]);
}
i = l = null;
}
};
root.imgLightbox = imgLightbox;
})("undefined" !== typeof window ? window : this, document);
(function(root) {
"use strict";
if (root.imgLightbox) {
imgLightbox("img-lightbox-link", {
onCreated: function() {
/* show your preloader */
},
onLoaded: function() {
/* hide your preloader */
},
onError: function() {
/* hide your preloader */
},
onClosed: function() {
/* hide your preloader */
},
rate: 500 /* default: 500 */
});
}
})("undefined" !== typeof window ? window : this);
Also see: Tab Triggers