Skip to content

Instantly share code, notes, and snippets.

@egfx
Forked from anonymous/index.html
Created June 6, 2017 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egfx/3ef95218affe60d52fad32b7065112e4 to your computer and use it in GitHub Desktop.
Save egfx/3ef95218affe60d52fad32b7065112e4 to your computer and use it in GitHub Desktop.
Simple Lightbox
<head>
<link href="https://fonts.googleapis.com/css?family=Playfair+Display" rel="stylesheet">
</head>
<section class="galleryWrapper applyEvent">
<h1 class="header">Click Image to Enlarge</h1>
<div class="imageContainer clearfix">
<a class="imageLink" href="https://www.dropbox.com/s/huauyagks8n4xvh/bagpipes-1600.jpg?raw=1" title="Man playing the bagpipes">
<img class="image" src="https://www.dropbox.com/s/huauyagks8n4xvh/bagpipes-1600.jpg?raw=1" alt="Man playing the babpipes">
</a>
<a class="imageLink" href="https://www.dropbox.com/s/bjqwj4dqjt43ayv/berlin-cathedral-1600.jpg?raw=1" title="Berlin Cathedral">
<img class="image" src="https://www.dropbox.com/s/bjqwj4dqjt43ayv/berlin-cathedral-1600.jpg?raw=1" alt="Berlin Cathedral">
</a>
<a class="imageLink" href="https://www.dropbox.com/s/4vzfoyutt0bz3ji/mountain-lake-1600.jpg?raw=1" title="Mountain lake">
<img class="image" src="https://www.dropbox.com/s/4vzfoyutt0bz3ji/mountain-lake-1600.jpg?raw=1" alt="Mountain lake">
</a>
<a class="imageLink" href="https://www.dropbox.com/s/qxy5jnqereb9to6/machu-picchu-1600.jpg?raw=1" title="Machu-picchu">
<img class="image" src="https://www.dropbox.com/s/qxy5jnqereb9to6/machu-picchu-1600.jpg?raw=1" alt="Machu-picchu">
</a>
</div> <!-- /imageContainer-->
<div class="overlayContainer">
<div class="imageBox">
<div class="relativeContainer">
<img class="largeImage" src="" alt="">
<p class="imageCaption"></p>
</div> <!-- /relativeContainer -->
</div> <!-- /imageBox -->
</div> <!-- overlayContainer -->
</section> <!-- /galleryWrapper -->
(function() {
var image = document.querySelectorAll('.imageLink'),
overlay = document.querySelector('.overlayContainer'),
largeImage = document.querySelector('.largeImage'),
applyEvent = document.querySelector('.applyEvent'),
caption = document.querySelector('.imageCaption'),
display = new RegExp('(^| )' + 'display' + '($| )', 'g'),
opacity = new RegExp('(^| )' + 'opacity' + '($| )', 'g'),
href,
alt;
function lightbox(event) {
event.preventDefault();
href = this.getAttribute('href');
alt = this.children[0].getAttribute('alt');
largeImage.setAttribute('src', href);
largeImage.setAttribute('alt', alt);
caption.innerHTML = alt;
overlay.className += ' display';
setTimeout(function() { overlay.className += ' opacity'; }, 25);
setTimeout(function() { overlay.addEventListener('click', hideOverlay, false); }, 400);
}
function hideOverlay() {
overlay.removeEventListener('click', hideOverlay, false);
overlay.className = overlay.className.replace(opacity, '');
setTimeout(function() {
largeImage.removeAttribute('src');
largeImage.removeAttribute('alt');
overlay.className = overlay.className.replace(display, '');
}, 400);
}
/***Event Listener***/
if ( applyEvent ) {
for ( var i = 0; i < image.length; i++ ) {
image[i].addEventListener('click', lightbox, false);
}
}
})();
* {
box-sizing: border-box;
}
html {
font-size: 100%;
}
body {
word-wrap: break-word;
}
.body,
.imageCaption {
line-height: 1.4;
}
.clearfix::after {
content: "";
display: table;
clear: both;
}
.galleryWrapper {
font-size: 0.85rem;
width: 90%;
margin: 0 auto;
}
.galleryWrapper,
.imageBox {
max-width: 60rem;
}
.header {
font: 2.1em 'Playfair Display', serif;
margin: 1.25rem 0;
}
.imageBox,
.header {
text-align: center;
}
.imageContainer {
padding: 0 0.5rem 1.25rem;
}
.imageLink {
display: inline-block;
}
.imageLink,
.imageBox {
line-height: 0;
}
.imageLink:nth-of-type(-n+3) {
margin-bottom: 0.8rem;
}
.imageBox,
.largeImage,
.image,
.imageCaption {
width: 100%;
}
.image,
.largeImage {
-webkit-transform: perspective(1px);
-moz-transform: perspective(1px);
transform: perspective(1px);
}
.overlayContainer {
display: none;
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 250;
}
.overlayContainer,
.imageCaption {
bottom: 0;
}
.relativeContainer {
position: relative;
margin: 0 0.5rem;
}
.imageBox,
.imageCaption {
position: absolute;
}
.imageBox {
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
.display {
display: block;
}
.imageCaption {
margin: 0;
font-size: 1.3em;
padding: 0.325rem;
}
/***Colors***/
body {
color: #333;
}
.galleryWrapper {
background-color: #f2f2f2;
border: 1px solid #333;
}
.imageLink {
outline: none;
-webkit-transition: all ease .2s;
-moz-transition: all ease .2s;
-o-transition: all ease .2s;
transition: all ease .2s;
}
.imageLink:hover,
.imageLink:focus {
opacity: 0.8;
}
.overlayContainer {
visibility: hidden;
opacity: 0;
background-color: rgba(51, 51, 51, .9);
-webkit-transition: all ease .5s;
-moz-transition: all ease .5s;
-o-transition: all ease .5s;
transition: all ease .5s;
}
.imageBox {
cursor: pointer;
}
.largeImage {
-webkit-box-shadow: 5px 5px 5px 0px rgb(0, 0, 0);
-moz-box-shadow: 5px 5px 5px 0px rgb(0, 0, 0);
box-shadow: 5px 5px 5px 0px rgb(0, 0, 0);
}
.imageCaption {
background: rgba(51, 51, 51, .8);
color: #fff;
}
.opacity {
visibility: visible;
opacity: 1;
}
@media only screen and (min-width: 30em) {
.imageLink {
float: left;
width: 48.33%;
}
.imageLink:nth-of-type(1),
.imageLink:nth-of-type(3) {
margin-right: 3.33%;
}
.imageLink:nth-of-type(3) {
margin-bottom: 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment