-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
44 lines (39 loc) · 1.65 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* Selecting the HTML elements */
const displayedImage = document.querySelector('.displayed-img');
const thumbBar = document.querySelector('.thumb-bar');
const btn = document.querySelector('button');
const overlay = document.querySelector('.overlay');
/* Declaring the array of image filenames */
const imgArr = ["pic1.jpg", "pic2.jpg", "pic3.jpg", "pic4.jpg", "pic5.jpg"];
/* Declaring the alternative text for each image file */
const imgAlts = [
{altText: "Photo 1 - Mountain Trail Path"},
{altText: "Photo 2 - Mountain Peak and Trail"},
{altText: "Photo 3 - Orange Flowers"},
{altText: "Photo 4 - Desert Mountain View"},
{altText: "Photo 5 - Sunrise in the Desert"}
];
/* Looping through images */
for (let i = 0; i < imgArr.length; i++) {
const newImage = document.createElement('img');
newImage.setAttribute('src', `images/${imgArr[i]}`);
newImage.setAttribute('alt', imgAlts[i].altText);
thumbBar.appendChild(newImage);
}
/* Set the src and alt attributes on the large image when a thumbnail is clicked */
thumbBar.addEventListener("click", (event) => {
displayedImage.setAttribute('src', event.target.getAttribute('src'));
displayedImage.setAttribute('alt', event.target.getAttribute('alt'));
});
/* Wiring up the Darken/Lighten button */
btn.addEventListener("click", () => {
if (btn.getAttribute("class") === "dark") {
btn.setAttribute("class", "light");
btn.textContent = "Lighten";
overlay.style.backgroundColor = "rgb(0 0 0 / 50%)";
} else {
btn.setAttribute("class", "dark");
btn.textContent = "Darken";
overlay.style.backgroundColor = "rgb(0 0 0 / 0%)";
}
});