JSFiddle - React, Tailwind, and code Playground
HTML
<!-- Loader -->
<div class="loader hidden">
<img src="rocket.svg" alt="Rocket Landing Animation">
</div>
<!-- Container -->
<div class="container">
<!-- Navigation -->
<div class="navigation-container">
<span class="background"></span>
<!-- Results Nav-->
<span class="navigation-items" id="resultsNav">
<h3 class="clickable">Favourites</h3>
<h3> • </h3>
<h3 class="clickable">Load More</h3>
</span>
<!-- Favourites Nav -->
<span class="navigation-items hidden" id="favouritesNav">
<h3 class="clickable">Load More images</h3>
</span>
</div>
<!-- Images Container -->
<div class="images-container">
</div>
</div>
<!-- Save Confirmation -->
<div class="save-confirmed" hidden>
<h1>ADDED!</h1>
</div>
CSS
html {
box-sizing: border-box;
}
body {
margin: 0;
background: whitesmoke;
overflow-x: hidden;
font-family: Verdana, sans-serif;
font-size: 1rem;
line-height: 1.8rem;
}
.container {
position: relative;
display: flex;
flex-direction: column;
align-items: center;
margin-top: 5px;
margin-bottom: 25px;
}
/* Loader */
.loader {
background: whitesmoke;
height: 100vh;
width: 100vw;
position: fixed;
z-index: 10;
display: flex;
justify-content: center;
align-items: center;
}
/* Navigation */
.navigation-container {
position: fixed;
top: 0;
}
.navigation-items {
display: flex;
justify-content: center;
}
.background {
background: whitesmoke;
position: fixed;
right: 0;
width: 100%;
height: 60px;
z-index: -1;
}
.clickable {
color: dodgerblue;
cursor: pointer;
user-select: none;
}
.clickable:hover {
color: rgb(13, 115, 218);
}
/* Images Container */
.images-container {
width: 800px;
margin-top: 50px;
}
.card {
background: white;
margin: 10px 10px 20px;
border-radius: 5px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
transition: 0.3s;
}
.card:hover {
box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
}
.card-img-top {
width: 100%;
height: auto;
border-radius: 5px 5px 0 0;
}
.card-body {
padding: 20px;
}
.card-title {
margin: 10px auto;
font-size: 24px;
}
/* Save Confirmation */
.save-confirmed {
background: white;
padding: 8px 16px;
border-radius: 5px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
transition: 0.3s;
position: fixed;
bottom: 25px;
right: 50px;
}
/* Hidden */
.hidden {
display: none;
}
/* Large Smartphone (Vertical) */
@media screen and (max-width: 800px) {
body {
line-height: 1.4rem;
font-size: 0.9rem;
}
.images-container {
width: 100%;
}
.card-title {
font-size: 20px;
}
.save-confirmed {
right: 25px;
}
}
JavaScript
const resultsNav = document.getElementById('resultsNav');
const favouritesNav = document.getElementById('favouritesNav');
const imagesContainer = document.querySelector('.images-container');
const saveConfirmed = document.querySelector('.saveConfirmed');
const loader = document.querySelector('.loader');
// Unsplash API
const count = 10;
const apiKey = 'DEMO KEY';
const apiUrl = `https://api.unsplash.com/photos/random?client_id=${apiKey}&count=${count}`;
let resultsArray = [];
function updateDOM() {
resultsArray.foreach((result) => {
// Card Container
const card = document.createElement('div');
card.classList.add('card');
// link
const link = document.createElement('a');
link.href = result.hdurl;
//Images
const image = document.createElement('img');
image.src = result.url;
image.alt = 'Image';
image.loading = 'lazy';
image.classList.add('card-img-top');
//Card Body
const cardBody = document.createElement('div');
cardBody.classList.add('card-body');
// Save Text
const saveText = document.createElement('p');
saveText.classList.add('clickable');
saveText.textContent = 'Add To Favourites';
// Append
cardBody.append(saveText);
link.appendChild(image);
card.append(link, cardBody);
imagesContainer.appendChild(card);
});
}
// Get 10 Images from Unsplash API
async function getUnplashPictures() {
try {
const response = await fetch(apiUrl);
resultsArray = await response.json();
console.log(resultsArray);
updateDOM();
} catch (error) {
// Catch Error Here
}
}
// On Load
getUnplashPictures();