Skip to content

Instantly share code, notes, and snippets.

@scythargon
Created February 8, 2015 02:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save scythargon/589fdf8cf5b8b435c680 to your computer and use it in GitHub Desktop.
Save scythargon/589fdf8cf5b8b435c680 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CodePen - Modern HTML5 Lightbox in 12 Lines of JavaScript</title>
<style>
dialog {
position: fixed;
left: 50%;
top: 50%;
-webkit-transform: translate(-50%, -50%);
margin: auto;
border: solid;
padding: 1em;
background: white;
color: black;
display: none;
opacity: 0;
border: none;
}
dialog[open] {
display: block;
}
.backdrop {
position: fixed;
background-color: rgba(0,0,0,0.1);
top: 0;
bottom: 0;
left: 0;
right: 0;
}
@-webkit-keyframes fadeToNearBlack {
to { background: rgba(0,0,0,0.4); }
}
@keyframes fadeToNearBlack {
to { background: rgba(0,0,0,0.4); }
}
@-webkit-keyframes goBig {
to { opacity: 1; }
}
@keyframes goBig {
to { opacity: 1; }
}
body { min-height: 100vh; }
nav#thumbs {
display: -webkit-box;
display: -webkit-flex;
display: flex;
}
nav#thumbs a {
display: block; -webkit-box-flex: 1; -webkit-flex: 1; -ms-flex: 1; flex: 1;
}
nav#thumbs a img {
width: 100%; height: auto;
}
dialog button {
border: none;
background: none;
font-size: 1.2rem;
}
dialog[open] {
-webkit-animation: goBig .6s forwards;
max-width: 700px;
}
dialog[open]::backdrop {
-webkit-animation: fadeToNearBlack .6s forwards;
}
.backdrop {
-webkit-animation: fadeToNearBlack .6s forwards;
}
dialog img {
width: 100%; height: auto;
}
</style>
</head>
<body>
<nav id="thumbs">
<a href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/elephant.jpg"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/elephant-thumb.jpg" alt=""></a>
<a href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/taj-mahal.jpg"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/taj-mahal-thumb.jpg" alt=""></a>
<a href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/wise-man.jpg"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/wise-man-thumb.jpg" alt="wise-man-thumb"></a>
</nav>
<dialog id="cover">
<img src="" alt="">
<button id="closecover">Close</button>
</dialog>
<script src="http://demosthenes.info/assets/scripts/extras/dialog-polyfill.js"></script>
<script>
function close_dialog() {
coverimage.setAttribute('src', '');
cover.close();
document.removeEventListener('click', close_dialog);
}
function showImage(e) {
e.preventDefault();
e.stopPropagation();
coverimage.setAttribute('src', this.getAttribute('href'));
coverimage.setAttribute('alt', this.querySelector('img').getAttribute('alt'));
cover.showModal();
document.addEventListener('click', close_dialog);
}
var imglinks = document.getElementById('thumbs').getElementsByTagName('a'), cover = document.getElementById('cover'), coverimage = cover.getElementsByTagName('img')[0], testdialog = document.createElement('dialog');
testdialog.setAttribute('open', '');
if (!testdialog.open) {
dialogPolyfill.registerDialog(cover);
}
for (var i = 0; i < imglinks.length; i++) {
imglinks[i].onclick = showImage;
}
//@ sourceURL=pen.js
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment