hello, everyone, I had just finished the assessment ‘Image gallery’, could someone make some comments about my code? thanks.
let displayedImage = document.querySelector('.displayed-img');
let thumbBar = document.querySelector('.thumb-bar');
let btn = document.querySelector('button');
let overlay = document.querySelector('.overlay');
function setDisplayedImage(src) {
displayedImage.setAttribute('src', src);
}
/* Looping through images */
for(let i = 1; i <= 5; ++i) {
let newImage = document.createElement('img');
newImage.setAttribute('src', 'images/pic' + i + '.jpg');
thumbBar.appendChild(newImage);
newImage.onclick = function(e) {
setDisplayedImage(e.target.getAttribute('src'));
}
}
/* Wiring up the Darken/Lighten button */
btn.onclick = function(e) {
let btnClass = e.target.getAttribute('class');
if(btnClass == 'dark') {
e.target.setAttribute('class', 'light');
e.target.textContent = 'Lighten';
overlay.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';
}
else {
e.target.setAttribute('class', 'dark');
e.target.textContent = 'Darken';
overlay.style.backgroundColor = 'rgba(0, 0, 0, 0)';
}
}