Created
May 22, 2019 14:04
-
-
Save petermolnar/b5bd11d202ca0eec68a28d39c89b1004 to your computer and use it in GitHub Desktop.
extremely simple vanilla js lightbox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var links2img = [] | |
function initLightbox() { | |
var links = document.getElementsByTagName("a"); | |
for(var i = links.length; i--; ) { | |
var imginside = links[i].getElementsByTagName("img"); | |
if (imginside.length == 1 ) { | |
links2img.push(links[i]) | |
console.log(links[i]); | |
links[i].onclick = openLightbox; | |
} | |
} | |
} | |
function openLightbox(e) { | |
var lightbox = document.createElement('div'); | |
lightbox.style.width = "98%"; | |
lightbox.style.height = "98%"; | |
lightbox.style.padding = "1%"; | |
lightbox.style.position = "fixed"; | |
lightbox.style.top = "0"; | |
lightbox.style.left = "0"; | |
lightbox.style.display = "table"; | |
lightbox.style.zIndex = "999"; | |
lightbox.style.backgroundColor = "rgba(0, 0, 0, 0.7)"; | |
lightbox.addEventListener('click', function(){ | |
closeLightbox(lightbox); | |
}); | |
var fig = document.createElement('figure'); | |
fig.style.display = "table-cell"; | |
fig.style.align = "center"; | |
fig.style.verticalAlign = "middle"; | |
var img = document.createElement('img'); | |
img.src = e.target.parentNode.href; | |
fig.appendChild(img); | |
lightbox.appendChild(fig); | |
e.target.parentNode.appendChild(lightbox); | |
return false; | |
} | |
function closeLightbox(t) { | |
console.log(t); | |
t.parentNode.removeChild(t); | |
return false; | |
} | |
initLightbox(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks ! Great job.