Created
February 25, 2021 05:43
-
-
Save gchib00/c404e2d6f7b9b51507b2c332b32b7bf5 to your computer and use it in GitHub Desktop.
Image gallery
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
const displayedImage = document.querySelector('.displayed-img'); | |
const thumbBar = document.querySelector('.thumb-bar'); | |
const btn = document.querySelector('button'); | |
const overlay = document.querySelector('.overlay'); | |
/* Looping through images */ | |
for (let i=1; i<=5; i++){ | |
const newImage = document.createElement('img'); | |
newImage.setAttribute('src', "pic"+i+".jpg"); | |
thumbBar.appendChild(newImage); | |
newImage.addEventListener("click", function(){ | |
document.getElementById("mainImage").src = "pic"+i+".jpg"; | |
}); | |
} | |
/* Wiring up the Darken/Lighten button */ | |
let currentState = btn.getAttribute("class"); | |
if (currentState === "dark"){ | |
btn.addEventListener("click", function() { | |
btn.textContent = "Lighten"; | |
btn.setAttribute("class", "light"); | |
overlay.style.backgroundColor="rgba(0,0,0,0.5)"; | |
}); | |
} | |
if (currentState === "light"){ | |
btn.addEventListener("click", function() { | |
btn.textContent = "Darken"; | |
btn.setAttribute("class", "dark"); | |
overlay.style.backgroundColor="rgba(0,0,0,0)"; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: I didn't touch the CSS/HTML file, except I just added an ID to the <img class="displayed-img" id="mainImage" src="images/pic1.jpg">