Skip to content

Instantly share code, notes, and snippets.

@pkage
Last active April 13, 2020 05:54
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 pkage/b78859756edee08c5de85d4bc5b0dea1 to your computer and use it in GitHub Desktop.
Save pkage/b78859756edee08c5de85d4bc5b0dea1 to your computer and use it in GitHub Desktop.
clicky clicky
<!DOCTYPE html>
<html>
<head>
<title>foo</title>
</head>
<body>
<img id="big"/>
<div id="thumbs"></div>
<style>
.thumb {
width: 100px;
height: 100px;
/* so it's obviously clickable */
cursor: pointer;
}
#big {
width: 300px;
height: 300px;
}
#thumbs {
/* another option would be CSS grid */
display: flex;
flex-direction: row;
align-items: flex-start;
justify-content: flex-start;
flex-wrap: wrap;
}
</style>
<script>
window.onload = async () => {
// request your bundle of images
let images = await fetch('url/to/your/json')
// wait for the json to resolve
images = await images.json()
// save globally
window.images = images
// populate the image list
const addImage = src => {
// helper to make an image element
let img = document.createElement('img')
img.setAttribute('src', 'data:image/png;base64,' + src)
img.classList.add('thumb')
// on click, set the big image to this one's source
img.addEventListener('click', () => {
document.querySelector('img#big')
.setAttribute('src', img.getAttribute('src'))
})
document.querySelector('#thumbs')
.appendChild(img)
}
// assuming it's an array, you may need to tweak this
for (let image of images.list) {
addImage(image.src)
}
}
</script>
<!-- ayy -->
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment