Hi Guys,
Iām working through the JS course and have just finished the img gallery project.
I had been getting through the course and itās exercises rather quickly until I got to this one. Found it to be pretty challenging and frustrating at times but I managed to sort it out after 3 days working on it.
Here is my solution:
var displayedImage = document.querySelector(ā.displayed-imgā);
var thumbBar = document.querySelector(ā.thumb-barā);
var btn = document.querySelector(ābuttonā);
var overlay = document.querySelector(ā.overlayā);
/* Looping through images */
var imgArray = [āimages\pic1.jpgā, āimages\pic2.jpgā, āimages\pic3.jpgā, āimages\pic4.jpgā, āimages\pic5.jpgā];
for (var i = 0; i < imgArray.length; i++) {
var newImage = document.createElement(āimgā);
newImage.setAttribute(āsrcā, imgArray[i]);
thumbBar.appendChild(newImage);
function imgDisplay(e) {
var newSrc = e.target.getAttribute(āsrcā);
displayedImage.setAttribute(āsrcā, newSrc);
}
newImage.addEventListener(āclickā, imgDisplay);
}
/* Wiring up the Darken/Lighten button */
function btnAction() {
var btnClassName = btn.getAttribute(āclassā);
if (btnClassName === ādarkā) {
btn.setAttribute(āclassā, ālightā);
btn.textContent = ālightenā;
overlay.style.backgroundColor = ārgba(0,0,0,0.5)ā;
} else {
btn.setAttribute(āclassā, ādarkā);
btn.textContent = ādarkenā;
overlay.style.backgroundColor = ārgba(0,0,0,0)ā;
}
}
btn.addEventListener(āclickā, btnAction);