Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@AVGP
Created December 3, 2018 20:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AVGP/1ef95b1860c5583b44854fa8f45a12ba to your computer and use it in GitHub Desktop.
Save AVGP/1ef95b1860c5583b44854fa8f45a12ba to your computer and use it in GitHub Desktop.
Stupidest, simplest JS web app ever
<!doctype html>
<html lang="en">
<head>
<title>Kitten Corner</title>
<meta charset="utf-8">
<meta name="description" content="Kitten Corner has a bunch of lovely cat pictures ready for you!">
<meta name="viewport" content="width=device-width">
<style>
body {
font-family: Helvetica, Arial, sans-serif;
}
ul {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-gap: 1em;
margin: 0;
padding: 0;
}
li {
display: block;
}
li img {
object-fit: cover;
width: 100%;
height: 100%;
}
h1, p {
font-size: 2rem;
text-align: center;
}
button {
border: 1px solid lightblue;
padding: 0.25em 1em;
background: rebeccapurple;
color: white;
font-size: 1.25rem;
}
</style>
</head>
<body>
<h1>Kitten Corner</h1>
<p><button>Get more kittens!</button></p>
<ul>
</ul>
<template>
<li>
<img alt="">
</li>
</template>
<script>
const apiUrl = 'https://api.thecatapi.com/v1/images/search?limit=50';
const tpl = document.querySelector('template').content;
const container = document.querySelector('ul');
function init () {
fetch(apiUrl)
.then(response => response.json())
.then(cats => {
container.innerHTML = '';
cats
.map(cat => {
const li = document.importNode(tpl, true);
li.querySelector('img').src = cat.url;
return li;
}).forEach(li => container.appendChild(li));
})
}
init();
document.querySelector('button').addEventListener('click', init);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment