Last active
October 24, 2021 03:07
-
-
Save kissarat/926e9642b48ca22c6cf8ce05ba403701 to your computer and use it in GitHub Desktop.
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Nginx Photo Viewer</title> | |
<style> | |
ul { | |
padding-left: 0; | |
display: flex; | |
flex-wrap: wrap; | |
} | |
li { | |
list-style: none; | |
padding: 4px; | |
width: 200px; | |
height: 200px; | |
text-align: center; | |
} | |
li.selected { | |
background-color: gray; | |
} | |
li.selected img { | |
opacity: 0.6; | |
} | |
img { | |
max-width: 100%; | |
max-height: 100%; | |
} | |
#errorMessage { | |
color: red; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="errorMessage"></div> | |
<ul></ul> | |
<script> | |
async function loadImages(ul) { | |
try { | |
const r = await fetch('/images') | |
const items = await r.json() | |
const fragment = document.createDocumentFragment() | |
for (const item of items) { | |
if ('file' === item.type && /\.jpg$/.test(item.name)) { | |
const li = document.createElement('li') | |
li.innerHTML = `<img src="/images/${item.name}" alt="${item.name}" />` | |
li.addEventListener('click', function (e) { | |
e.currentTarget.classList.toggle('selected') | |
}) | |
fragment.appendChild(li) | |
} | |
} | |
ul.appendChild(fragment) | |
} catch (err) { | |
errorMessage.innerHTML = err.message || err.toString() | |
} | |
} | |
window.addEventListener('DOMContentLoaded', function () { | |
const ul = document.querySelector('ul') | |
loadImages(ul) | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment