Skip to content

Instantly share code, notes, and snippets.

@gchib00
Created February 25, 2021 05:43
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 gchib00/c404e2d6f7b9b51507b2c332b32b7bf5 to your computer and use it in GitHub Desktop.
Save gchib00/c404e2d6f7b9b51507b2c332b32b7bf5 to your computer and use it in GitHub Desktop.
Image gallery
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)";
});
}
@gchib00
Copy link
Author

gchib00 commented Feb 25, 2021

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">

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment